2011-11-05 64 views
5

如何上傳圖像文件到使用SOAP web服務?上傳圖像的WebService

我需要用它來SoapObejct使用這樣的Web服務可以處理在其上下文輸入文件,並給它一個新的文件名。

怎麼樣? 任何代碼示例?

約阿夫

回答

7

轉換圖像文件到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的字符串作爲第二個參數。

+0

OK,怎麼可以將其轉換爲Base64?以及如何轉換回圖像文件在服務器上?我發現使用DataHandler的信息很少 - 哪個更好?一個不錯的短代碼例子會非常棒。謝謝 – Yoav

+0

Gson是否可以與SoapObject一起使用?我正在使用SoapObject c; ... c.adppProperty(...)。我應該將Base^$ info作爲屬性嗎? – Yoav

+0

你如何實現你的網絡服務?它是一個.net web服務嗎? – breceivemail