2011-09-09 32 views
1

我試着從Android文件流式傳輸到一個asp.net服務:流從Android文件到.NET HTTP服務

private static void writeFile(File file, DataOutputStream out) 
     throws IOException { 

    BufferedInputStream bufferedFileIs = null; 
    Base64OutputStream base64out = null; 
    try { 
     out.writeBytes("fileBase64="); 
     base64out = new Base64OutputStream(out, Base64.DEFAULT); 

     FileInputStream fileIs = new FileInputStream(file); 
     bufferedFileIs = new BufferedInputStream(fileIs); 
     int nextByte; 
     while ((nextByte = bufferedFileIs.read()) != -1) { 
      base64out.write(nextByte); 
     } 
    } finally { 
     if (bufferedFileIs != null) { 
      bufferedFileIs.close(); 
     } 
     if(base64out != null) 
      base64out.flush(); 

    } 

} 

,並收到類似這樣的

String base64 = Request.Form["fileBase64"]; 

byte[] bytes = System.Convert.FromBase64String(base64); 

我使用HttpURLConnection的我沒有得到任何例外,但收到的文件(圖像)在這個過程中被破壞。 我嘗試了很多不同的流包裝對,但沒有運氣。任何人都有這方面的經驗? 我流其他形式的條目在同一個連接,這些到達未損壞,例如

&UserID=12345 

Gratefull對您有所幫助。

乾杯!

回答

3

解決它:

準備文件:

File file = new File(LocalFilePath); 

FileEntity fileentity = new FileEntity(file, "UTF-8"); 
HttpUtilities.postRequest(WebServiceURL, fileentity); 

發佈要求:

public static String postRequest(String url, HttpEntity aEntity) 
     throws IOException { 

    InputStream is = null; 
    try { 

     HttpClient client = new DefaultHttpClient(); 

     HttpPost httppost = new HttpPost(url); 

     httppost.setEntity(aEntity); 

     HttpResponse response = client.execute(httppost); 
     HttpEntity responseEntity = response.getEntity(); 

     is = responseEntity.getContent(); 

    } catch (Exception e) { 
    } 

    return getResponse(is); 
} 

在此之後,網絡服務器抱怨:

HttpException (0x80004005): Maximum request length exceeded 

最大請求長度我默認爲4MB在web.config中設置:

<system.web> 
    <httpRuntime maxRequestLength="1048576"/> 
</system.web 

它允許文件高達1GB(!)。

編輯:

忘記服務器代碼:

var str = Request.InputStream; 
strLen = Convert.ToInt32(str.Length); 
byte[] strArr = new byte[strLen]; 
strRead = str.Read(strArr, 0, strLen); 
相關問題