Android HttpURLConnection POST 请求 image/jpeg
Android HttpURLConnection POST Request with image/jpeg
我正在尝试使用 HttpURLConnection
在 POST 请求中发送图像。我有下一个代码:
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();
但是当我尝试获取连接应用程序的响应 code/message 时抛出异常:
07-28 12:15:00.722: java.io.IOException: unexpected end of stream on
Connection{123.456.789.012:80, proxy=DIRECT@ hostAddress=123.456.789.012
cipherSuite=none protocol=http/1.1} (recycle count=0)
也许问题是我的 post 请求错误或有语法错误?能不能帮我说说哪里不对啊
我在 windows 上的 Java 应用程序上测试了这段代码!请求结果为200 OK
。结论:android 有问题!但是我还是不知道具体是什么。
我找到了解决办法!这个丑陋的字符串 System.lineSeparator()
需要用旧的 \r\n
替换!太好了。
我正在尝试使用 HttpURLConnection
在 POST 请求中发送图像。我有下一个代码:
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();
但是当我尝试获取连接应用程序的响应 code/message 时抛出异常:
07-28 12:15:00.722: java.io.IOException: unexpected end of stream on Connection{123.456.789.012:80, proxy=DIRECT@ hostAddress=123.456.789.012 cipherSuite=none protocol=http/1.1} (recycle count=0)
也许问题是我的 post 请求错误或有语法错误?能不能帮我说说哪里不对啊
我在 windows 上的 Java 应用程序上测试了这段代码!请求结果为200 OK
。结论:android 有问题!但是我还是不知道具体是什么。
我找到了解决办法!这个丑陋的字符串 System.lineSeparator()
需要用旧的 \r\n
替换!太好了。