上传图片从机器人到web服务人到、上传图片、机器、web

2023-09-12 09:55:20 作者:爽到极点自然叫

使用SOAP我如何上传图片文件到web服务?

How can I upload an image file to a webservice using SOAP?

我需要它与SoapObejct使用这样的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.

如何? 任何code的例子吗?

How? Any code examples?

约阿夫

推荐答案

图像文件转换为Base64的字符串,其名称的网络服务很容易地把你的字符串。 你还需要code样?

Convert your image file to a Base64 string and send your string with its name to web-service easily. Do you still need code sample?

修改

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;
}

把你的对象给web服务作为一个JSON字符串:

send your object to the webservice as a JSON string:

在您的活动:

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的副本的真正对象。

In your webservice convert your json string to a real object which is a replica of MyClass.

编辑:

你也可以忽略JSON和有webserivce方法有两个参数: MyWebserviceMethod(字符串的文件名,字符串内容); 通过Base64的字符串作为第二个参数。

Also you can ignore Json and have a webserivce method with 2 parameters: MyWebserviceMethod(string filename, string content); pass Base64 string as second parameter.