[英]upload image from android to webservice
How can I upload an image file to a webservice using SOAP? 如何使用SOAP将图像文件上传到Web服务?
I need it to be used with SoapObejct so the webservice can handle the input file in its context and give it a new filename. 我需要它与SoapObejct一起使用,因此webservice可以在其上下文中处理输入文件并为其提供新的文件名。
How? 怎么样? Any code examples?
任何代码示例?
Yoav 约阿夫
Convert your image file to a Base64 string and send your string with its name to web-service easily. 将您的图像文件转换为Base64字符串,并将您的字符串及其名称轻松发送到Web服务。 Do you still need code sample?
你还需要代码样本吗?
Edit 编辑
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;
}
send your object to the webservice as a JSON string: 将您的对象作为JSON字符串发送到Web服务:
in your activity: 在你的活动中:
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
//...
}
In your webservice convert your json string to a real object which is a replica of MyClass. 在您的web服务中,将您的json字符串转换为真实对象,该对象是MyClass的副本。
edit: 编辑:
Also you can ignore Json and have a webserivce method with 2 parameters: MyWebserviceMethod(string filename, string content);
你也可以忽略Json并使用带有2个参数的webserivce方法:
MyWebserviceMethod(string filename, string content);
pass Base64 string as second parameter. 传递Base64字符串作为第二个参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.