在post方法中上传https url中的文件 - java.io.IOException:写入服务器时出错 - Java [英] Uploading file in a https url in post method - java.io.IOException: Error writing to server - Java
问题描述
我必须使用多部分将二进制映像文件上传到服务器,我打开与https url的连接并直接写入输出流。我已经尝试过SSLSocket,apache http客户端,但在各方面我都收到错误消息,java.io.IOException:写入服务器时出错。我必须使用多部分二进制文件编写post params。代码如下,我在下面的代码中没有看到错误?而且,我已经提供了样本数据来将数据写入连接。我很高兴如果有人能用apache http客户端知道怎么做,我不知道。
I have to upload a binary image file to the server using multi-part, I open the connection to the https url and write diretly to to the outputstream. I have tried the SSLSocket, apache http client, but in all ways I get the error message, java.io.IOException: Error writing to server. I have to write post params with the multi-part binary file. The code is given below, where am I failing to see the mistake in this code below? And, I have given the sample data to write the data to the connection. I would be glad if anyone could give some idea how to do this with apache http client, which I do not know.
示例包数据
header part
--------------
--aabbaabbaabbxx
Content-Disposition: form-data; name="to"
<recipient>
--aabbaabbaabbxx
Content-Disposition: form-data; name="from"
<sender>
--aabbaabbaabbxx
Content-Disposition: form-data; name="file"; filename="<file_hash>.jpg"
Content-Type: image/jpeg
footer
-----------
--aabbaabbaabbxx--
Are these request parameters?
-----------------------------
POST <full_url>
Content-Type: multipart/form-data; boundary=aabbaabbaabbxx
Host: <host_name>
User-Agent: <agent>
Content-Length: <file_size>
代码
The code
final URL uri = new URL(uploadUrl);
String boundary = "aabbaabbaabbxx";
HttpURLConnection connection = (HttpURLConnection) uri.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setAllowUserInteraction(false);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
OutputStream writer = null;
try {
writer = connection.getOutputStream();
// post
writer.write(MessageFormat
.format("POST {0}\r\n", uploadUrl)
.getBytes("UTF-8"));
writer.write(MessageFormat
.format("Content-Type: multipart/form-data; boundary={0}\r\n",
boundary).getBytes("UTF-8"));
writer.write(MessageFormat.format("Host: {0}\r\n",uri.getHost()).getBytes("UTF-8"));
writer.write(MessageFormat.format("User-Agent: {0}\r\n",Constants.UserAgent).getBytes("UTF-8"));
writer.write(MessageFormat.format("Content-Length: {0}\r\n\r\n",clength).getBytes("UTF-8"));
// header
writer.write(("--" + boundary).getBytes("UTF-8"));
writer.write("Content-Disposition: form-data; name=\"to\"\r\n\r\n".getBytes("UTF-8"));
writer.write(MessageFormat.format("{0}\r\n", to).getBytes("UTF-8"));
writer.write(MessageFormat.format("--{0}\r\n", boundary).getBytes("UTF-8"));
writer.write("Content-Disposition: form-data; name=\"from\"\r\n\r\n".getBytes("UTF-8"));
writer.write(MessageFormat.format("{0}\r\n",this.phoneNumber).getBytes("UTF-8"));
writer.write(MessageFormat.format("--{0}\r\n", boundary).getBytes("UTF-8"));
writer.write(MessageFormat.format("Content-Disposition: form-data; name=\"file\"; filename=\"{0}\"\r\n",hashname).getBytes("UTF-8"));
writer.write(MessageFormat.format("Content-Type: {0}\r\n\r\n", contenttype).getBytes("UTF-8"));
// file data
InputStream is = null;
try {
is = new FileInputStream(path);
int data = 0;
while ((data = is.read()) != -1) {
writer.write(data);
}
} finally {
if (is != null) {
try {
is.close();
} catch (IOException logOrIgnore) {
}
}
}
// footer
writer.write(("--" + boundary + "--").getBytes("UTF-8"));
writer.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
writer.close();
}
}
异常
java.io.IOException:在sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:468)
sun.net.www上写入服务器
时出错.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:480)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1070)
at sun.net.www .protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
Exception
java.io.IOException: Error writing to server at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:468) at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:480) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1070) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
推荐答案
您可以找到块的基本示例编码POST在Apache HttpClient 示例页面。
You can find the basic example for a "chunk encoded POST" on the Apache HttpClient example page.
对于基于表单的帖子,您还需要 org.apache.httpcomponents:httpmime:4.3.2
有了这个你得到 org.apache.http.entity.mime.MultipartEntityBuilder
你可以这样使用:
For the form-based post you also need org.apache.httpcomponents:httpmime:4.3.2
With that you get the org.apache.http.entity.mime.MultipartEntityBuilder
which you can use like so:
MultipartEntityBuilder meb = MultipartEntityBuilder.create();
meb.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
meb.addPart("to", new StringBody("The recipient", ContentType.TEXT_PLAIN));
meb.addPart("from", new StringBody("The sender", ContentType.TEXT_PLAIN));
FileBody fb = new FileBody(new File("path/to/your/file"), ContentType.create("image/jpeg"));
meb.addPart("file", fb);
meb.addPart("footer", new StringBody("The footer", ContentType.TEXT_PLAIN));
httppost.setEntity(meb.build());
这应该会帮助你。
这篇关于在post方法中上传https url中的文件 - java.io.IOException:写入服务器时出错 - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!