Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Sending files over HTTP is supposed to take place using <code>multipart/form-data</code> encoding. Your servlet part is fine as it already uses <a href="http://commons.apache.org/fileupload" rel="nofollow noreferrer">Apache Commons FileUpload</a> to parse a <code>multipart/form-data</code> request.</p> <p>Your client part, however, is apparently not fine as you're seemingly writing the file content raw to the request body. You need to ensure that your client sends a proper <code>multipart/form-data</code> request. How exactly to do it depends on the API you're using to send the HTTP request. If it's plain vanilla <code>java.net.URLConnection</code>, then you can find a concrete example somewhere near the bottom of <a href="https://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests/2793153#2793153">this answer</a>. If you're using <a href="http://hc.apache.org/httpcomponents-client-ga/" rel="nofollow noreferrer">Apache HttpComponents Client</a> for this, then here's a concrete example:</p> <pre><code>HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); MultipartEntity entity = new MultipartEntity(); entity.addPart("file", new FileBody(file)); post.setEntity(entity); HttpResponse response = client.execute(post); // ... </code></pre> <hr> <p><strong>Unrelated</strong> to the concrete problem, there's a bug in your server side code:</p> <pre><code>File file = new File("\files\\"+item.getName()); item.write(file); </code></pre> <p>This will potentially overwrite any previously uploaded file with the same name. I'd suggest to use <a href="http://download.oracle.com/javase/6/docs/api/java/io/File.html#createTempFile%28java.lang.String,%20java.lang.String,%20java.io.File%29" rel="nofollow noreferrer"><code>File#createTempFile()</code></a> for this instead.</p> <pre><code>String name = FilenameUtils.getBaseName(item.getName()); String ext = FilenameUtils.getExtension(item.getName()); File file = File.createTempFile(name + "_", "." + ext, new File("/files")); item.write(file); </code></pre>
 

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