[英]How to pass byte array of an Image over a soap web-service(XML) and decode this byte array on android device to convert it back to an Image
[英]How to convert File to Byte array in Android that can be consumed in ASP.Net (SOAP) Web Service?
我在ASP.net(SOAP)中有一個簡單的文件上傳Web服務,這是代碼:
<WebMethod()> _
Public Function UploadFile(ByVal f As Byte(), ByVal fileName As String) As String
Dim result As String = Json.JsonConvert.SerializeObject(New Result("INFO"))
Dim ms As MemoryStream
Dim fs As FileStream
Try
ms = New MemoryStream(f)
fs = New FileStream(System.Web.Hosting.HostingEnvironment.MapPath("~/Upload/") & fileName, FileMode.Create)
ms.WriteTo(fs)
result = Json.JsonConvert.SerializeObject(New Result("Upload Successful", ""))
Catch ex As Exception
result = Json.JsonConvert.SerializeObject(New Result("Upload Failed", ex.Message))
MyCustomClass.Utilities.EventsLog.CreateEventsLog("FileDALC.asmx - UploadFile", ex.Message, ex.Source, "Services", Utilities.EventsLogType.Failed)
Finally
If Not IsNothing(ms) Then
ms.Close()
ms.Dispose()
End If
If Not IsNothing(fs) Then
fs.Close()
fs.Dispose()
End If
End Try
Return Result
End Function
我想要的只是從我的Android設備調用該Web服務並發送一些字節數組參數,但是我始終無法生成可被Web服務識別的字節數組。
這是我在Android設備中的代碼:
public class WebServiceUploadData {
private static String NAMESPACE = "http://services.chameleonmobile.app/";
private static String URL = "http://10.41.25.49/SERVICES/Chameleon.WS.TitaniumMobile/FileDALC.asmx";
private static String SOAP_ACTION = "http://services.chameleonmobile.app/";
public static String invokeUploadFile(byte[] _file, String _fileName, String webMethName) {
String resTxt = "";
SoapObject request = new SoapObject(NAMESPACE, webMethName);
PropertyInfo file = new PropertyInfo();
file.setName("f");
file.setValue(_file);
PropertyInfo fileName = new PropertyInfo();
fileName.setName("fileName");
fileName.setValue(_fileName);
fileName.setType(String.class);
request.addProperty(file);
request.addProperty(fileName);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
new MarshalBase64().register(envelope);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION+webMethName, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
resTxt = response.toString();
}
catch (Exception e) {
e.printStackTrace();
if (e.getMessage().contains("EHOSTUNREACH")){
resTxt += "Error occured (Method Not Found, Wrong Network) - " + e.getMessage();
}
else if(e.getMessage().contains("ENETUNREACH")){
resTxt += "Error occured (Network Offline and Unreachable) - " + e.getMessage();
}
else{
resTxt += "Error occured - " + e.getMessage();
}
}
return resTxt;
}
}
並在MainActivity.java中異步調用該函數。
我產生這樣的字節數組(uploadFile是將發送到Web服務的字節數組參數):
File dirFile = new File(Environment.getExternalStorageDirectory() + "/TempDownloadFile", "Halo.txt");
FileInputStream fis = new FileInputStream(dirFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum);
}
byte[] bytes = bos.toByteArray();
uploadFile = Base64.encode(bytes, Base64.DEFAULT);
並嘗試這個:
byte[] uploadData = FileUtils.readFileToByteArray(dirFile);//Convert any file, image or video into byte array
uploadFile = Base64.encode(uploadData, Base64.DEFAULT);//Base64.encodeToString(data, Base64.NO_WRAP);
和這個:
uploadFile = Base64.encode(IOUtils.toByteArray(fis), Base64.DEFAULT);
但是不幸的是,我嘗試過的所有方法都不適合我,並且我的soap Web服務仍然無法重新識別我的字節數組。
有誰知道如何解決這一問題? 謝謝..
我會嘗試使用諸如Fiddler之類的東西來記錄擊中Web服務的流量,以便您可以看到soap請求的實際外觀並將其發布。
我還考慮不將來自Internet的連接並置在一起,因為這會通過稱為目錄遍歷攻擊的技術使您的服務器處於危險之中:
Dim badFileName As String = "..\..\inetpub\wwwroot\default.aspx"
File.CreateText("C:\Users\a-h\" + badFileName)
該代碼段與您的代碼類似,將嘗試在C:\\ inetpub \\ wwwroot \\ default.aspx上創建一個文件,而不是我希望將其保存到的C:\\ Users \\ ah \\目錄。
最后,也許您會發現從Android使用Web API更容易,因為您可以發布HTTP請求來代替。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.