2016-07-28 66 views
3

我想發送POST請求中的圖像與HttpURLConnection。我有下面的代碼:Android HttpURLConnection POST請求與圖像/ jpeg

String i3 = String.valueOf(System.currentTimeMillis()); 
ByteArrayOutputStream i4 = new ByteArrayOutputStream(); 
i4.write(("--" + i3 + System.lineSeparator() 
     + "Content-Disposition: form-data; name=\"" + I1.this.i2 + "\"; filename=\"" + I1.this.i2 + ".jpg\"" + System.lineSeparator() 
     + "Content-Type: image/jpeg" + System.lineSeparator() 
     + "Content-Length: " + String.valueOf(i2.this.i1.length) + System.lineSeparator() + System.lineSeparator()).getBytes()); 
i4.flush(); 
i4.write(i2.this.i1); 
i4.flush(); 
i4.write((System.lineSeparator() + System.lineSeparator() + "--" + i3 + "--").getBytes()); 
i4.flush(); 
i4.close(); 
HttpURLConnection i5 = (HttpURLConnection) new URL(myUrl).openConnection(); 
i5.setRequestMethod("POST"); 
i5.setDoOutput(true); 
i5.setRequestProperty("User-Agent", "Mozilla/5.0"); 
i5.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + i3); 
i5.setFixedLengthStreamingMode(i4.toByteArray().length); 
i5.connect(); 
OutputStream i6 = i5.getOutputStream(); 
i6.write(i4.toByteArray()); 
i6.flush(); 
i6.close(); 
System.out.println(i5.getResponseCode() + " " + i5.getResponseMessage()); 
i5.disconnect(); 

但是,當我試圖得到響應代碼/連接的應用程序的消息引發了我的異常:

12月7日至28日:15:00.722:java.io .IOException:上 連接{123.456.789.012:80,代理= DIRECT @ hostAddress = 123.456.789.012 密碼組=無協議= HTTP/1.1}(再循環計數= 0)

也許問題流意外結束是我的帖子請求不好或有syn稅收錯誤?你能幫我,說出了什麼問題嗎?


我在Windows測試此代碼的Java應用程序!請求的結果是200 OK。結論:Android出錯了!但我仍然不知道什麼是具體的。

回答

0

我找到了解決方案!這個醜陋的字符串System.lineSeparator()需要更換舊的\r\n!大。