Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Don't use <code>ObjectOutputStream</code>. It's for writing serialized Java objects, not for writing raw binary data. It may indeed block the stream. Rather just write directly to the <code>OutputStream</code> of the URL connection.</p> <p>That said, the code looks pretty overcomplicated. Even after re-reading several times and blinking my eyes countless times, I can't get it right. I suggest you to send those files according the <code>multipart/form-data</code> encoding with help of <a href="http://hc.apache.org/httpcomponents-client/index.html" rel="nofollow noreferrer">Commons HttpClient</a>. You can find <a href="http://www.java-tips.org/other-api-tips/httpclient/how-to-use-multipart-post-method-for-uploading.html" rel="nofollow noreferrer">here</a> a basic code example. You just have to modify <code>Part[] parts</code> to include all the files. The servlet on the other side can in turn use <a href="http://commons.apache.org/fileupload" rel="nofollow noreferrer">Commons FileUpload</a> to parse the <code>multipart/form-data</code> request.</p> <p>To calculate the progress, I'd suggest to pick <a href="http://commons.apache.org/io/apidocs/org/apache/commons/io/output/CountingOutputStream.html" rel="nofollow noreferrer"><code>CountingOutputStream</code></a> of <a href="http://commons.apache.org/io" rel="nofollow noreferrer">Commons IO</a>. Just wrap the <code>OutputStream</code> with it and write to it.</p> <p><strong>Update:</strong> if you don't like to ship your applet with more 3rd party libraries (which I imagine is reasonable), then have a look at <a href="https://stackoverflow.com/questions/2477449/simple-stream-read-write-question-in-java/2478127#2478127">this code snippet</a> (and the original question mentioned as 1st link) how to create a <code>multipart/form-data</code> body yourself using <code>URLConnection</code>.</p>
 

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