上传图像的WebService
问题描述:
如何上传图像文件到使用SOAP web服务?上传图像的WebService
我需要用它来SoapObejct使用这样的Web服务可以处理在其上下文输入文件,并给它一个新的文件名。
怎么样? 任何代码示例?
约阿夫
答
转换图像文件到Base64字符串,并将其更名为网络服务轻松发送您的字符串。 你还需要代码示例吗?
编辑
public static String fileToBase64(String path) throws IOException {
byte[] bytes = Utilities.fileToByteArray(path);
return Base64.encodeBytes(bytes);
}
public static byte[] fileToByteArray(String path) throws IOException {
File imagefile = new File(path);
byte[] data = new byte[(int) imagefile.length()];
FileInputStream fis = new FileInputStream(imagefile);
fis.read(data);
fis.close();
return data;
}
public class MyImage {
public String name;
public String content;
}
发送你的对象的WebService作为一个JSON字符串:
在活动:
MyClass myClass = new MyClass();
myClass.name = "a.jpg";
myClass.content = fileToBase64("../../image.jpg");
sendMyObject(myClass);
private void sendMyObject(
MyImage myImage) throws Exception {
// create json string from your object
Gson gson = new Gson();
String strJson = gson.toJson(myImage);
//send your json string here
//...
}
在你的web服务的JSON字符串转换为真正的该对象是MyClass的复制品。
编辑:
您也可以忽略JSON和具有webserivce方法有两个参数:MyWebserviceMethod(string filename, string content);
通Base64的字符串作为第二个参数。
OK,怎么可以将其转换为Base64?以及如何转换回图像文件在服务器上?我发现使用DataHandler的信息很少 - 哪个更好?一个不错的短代码例子会非常棒。谢谢 – Yoav
Gson是否可以与SoapObject一起使用?我正在使用SoapObject c; ... c.adppProperty(...)。我应该将Base^$ info作为属性吗? – Yoav
你如何实现你的网络服务?它是一个.net web服务吗? – breceivemail