Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You'd normally use <a href="http://java.sun.com/javase/6/docs/api/java/net/URLConnection.html" rel="noreferrer"><code>java.net.URLConnection</code></a> to fire HTTP requests. You'd also normally use <a href="http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2" rel="noreferrer"><code>multipart/form-data</code></a> encoding for mixed POST content (binary and character data). Click the link, it contains information and an example how to compose a <code>multipart/form-data</code> request body. The specification is in more detail described in <a href="http://www.faqs.org/rfcs/rfc2388.html" rel="noreferrer">RFC2388</a>.</p> <p>Here's a kickoff example:</p> <pre><code>String url = "http://example.com/upload"; String charset = "UTF-8"; String param = "value"; File textFile = new File("/path/to/file.txt"); File binaryFile = new File("/path/to/file.bin"); String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value. String CRLF = "\r\n"; // Line separator required by multipart/form-data. URLConnection connection = new URL(url).openConnection(); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); try ( OutputStream output = connection.getOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true); ) { // Send normal param. writer.append("--" + boundary).append(CRLF); writer.append("Content-Disposition: form-data; name=\"param\"").append(CRLF); writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); writer.append(CRLF).append(param).append(CRLF).flush(); // Send text file. writer.append("--" + boundary).append(CRLF); writer.append("Content-Disposition: form-data; name=\"textFile\"; filename=\"" + textFile.getName() + "\"").append(CRLF); writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); // Text file itself must be saved in this charset! writer.append(CRLF).flush(); Files.copy(textFile.toPath(), output); output.flush(); // Important before continuing with writer! writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary. // Send binary file. writer.append("--" + boundary).append(CRLF); writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF); writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF); writer.append("Content-Transfer-Encoding: binary").append(CRLF); writer.append(CRLF).flush(); Files.copy(binaryFile.toPath(), output); output.flush(); // Important before continuing with writer! writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary. // End of multipart/form-data. writer.append("--" + boundary + "--").append(CRLF).flush(); } // Request is lazily fired whenever you need to obtain information about response. int responseCode = ((HttpURLConnection) connection).getResponseCode(); System.out.println(responseCode); // Should be 200 </code></pre> <p>This code is less verbose when you use a 3rd party library like Apache Commons <a href="http://hc.apache.org/" rel="noreferrer">HttpComponents Client</a>.</p> <p>The Apache Commons <a href="http://commons.apache.org/fileupload" rel="noreferrer">FileUpload</a> as some incorrectly suggest here is only of interest in the <strong>server side</strong>. You can't use and don't need it at the client side.</p> <h3>See also</h3> <ul> <li><a href="https://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests">Using java.net.URLConnection to fire and handle HTTP requests</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload