问题
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.
example packet data
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();
}
}
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)
回答1:
You can find the basic example for a "chunk encoded POST" on the Apache HttpClient example page.
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());
That should help you on your way.
来源:https://stackoverflow.com/questions/21759592/uploading-file-in-a-https-url-in-post-method-java-io-ioexception-error-writin