Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First, please check that the files you are uploading are the ones you think they are and that they do not have an empty content.</p> <p>Second, please use the servlet code below and run your example for both <code>http</code> and <code>https</code> protocols. You can change the variable <code>dumpRequest</code> to <code>false</code> to print information for your <code>FileItem</code>s found similar to the one you have.</p> <pre><code>public class UploadServlet extends HttpServlet { private boolean dumpRequest = true; private boolean saveToFile = true; private String fileName = "requestBody.txt"; @Override public void init() { log("Upload servlet initialized"); } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) { try { log("Servicing new request..."); if (dumpRequest) { dumpRequestBody(request, response); } else { Writer writer = new OutputStreamWriter( response.getOutputStream() ); FileItem item = null; DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List&lt;?&gt;items = upload.parseRequest(request); ListIterator&lt;?&gt; iterator = items.listIterator(); while (iterator.hasNext()) { item = (FileItem) iterator.next(); writer.write("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); writer.write("item.getContentType():\t " + item.getContentType() + "\n"); writer.write("item.getName:\t" + item.getName() + "\n"); writer.write("item.getSize:\t" + item.getSize() + "\n"); writer.write("item.getString:\t" + item.getString() + "\n"); writer.write("item.getFieldName:\t" + item.getFieldName() + "\n"); writer.write("item.isFormField:\t" + item.isFormField() + "\n"); writer.write("item.isInMemory:\t" + item.isInMemory() + "\n"); writer.write("item.toString():\t" + item.toString() + "\n"); writer.flush(); } writer.close(); } } catch(Exception e) { throw new RuntimeException(e); } } private void dumpRequestBody(HttpServletRequest request, HttpServletResponse response) throws IOException { OutputStream responseOut = response.getOutputStream(); InputStream in = request.getInputStream(); if (!saveToFile) { dumpHeaders(request, responseOut); dumpStream(in, responseOut); } else { File file = new File(fileName); FileOutputStream out = new FileOutputStream(file); dumpStream(in, out); out.close(); Writer writer = new OutputStreamWriter(responseOut); writer.write("Response body was saved to file: " + file.getAbsolutePath()); writer.close(); } in.close(); responseOut.flush(); responseOut.close(); } private void dumpHeaders(HttpServletRequest request, OutputStream out) throws IOException { Writer writer = new OutputStreamWriter(out); writer.write("["); // first dump headers Enumeration&lt;String&gt; allHeaders = request.getHeaderNames(); while (allHeaders.hasMoreElements()) { String header = allHeaders.nextElement(); String value = request.getHeader(header); writer.write(header + ":" + value + "\n"); } writer.write("]"); writer.flush(); } private void dumpStream(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[2 * 1024]; int bytesRead = 0; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } out.flush(); } } </code></pre> <p>The above servlet when running for <code>http</code> and a file named <code>test.txt</code> which contains a simple line of text <code>Test data</code> which is uploaded via a simple html page produces the dump below. Compare the outputs you get (please also update your question with those outputs) with the one below to be able to further troubleshoot this issue. </p> <pre class="lang-none prettyprint-override"><code>[host:localhost:8080 user-agent:Mozilla/5.0 (Windows NT 6.0; rv:22.0) Gecko/20100101 Firefox/22.0 accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 accept-language:el-gr,el;q=0.8,en-us;q=0.5,en;q=0.3 accept-encoding:gzip, deflate referer:http://localhost:8080/upload/upload.html connection:keep-alive content-type:multipart/form-data; boundary=---------------------------5118710224663 content-length:298 ]-----------------------------5118710224663 Content-Disposition: form-data; name="myfile"; filename="test.txt" Content-Type: text/plain Test data -----------------------------5118710224663 Content-Disposition: form-data; name="submit" Submit -----------------------------5118710224663-- </code></pre> <hr> <p><strong>Update 1:</strong> Servlet code and output updated. The new servlet has the option of writing the request body to a file. Please run twice with <code>dumpRequest = true</code>, <code>saveToFile = false</code> to see the result on the response and update your output in the question and once using <code>true</code> for both, to save the request body in a file. Please upload that file somewhere. </p> <p>As seen from the first output of the UploadServlet the file data exist in the request body but they are not parsed correctly by commons file upload. You are either hitting a bug (it wouldn't be the first time that <a href="http://commons.apache.org/proper/commons-fileupload/faq.html" rel="nofollow">commons upload and Flash don't play nice together</a>) if you are using the newest versions (i.e. commons-fileupload: 1.3, commons-io: 2.4) or if you are using an older version you are hitting that bug or a side effect of it.</p> <p>Because the most possible cause is that there is something wrong with the line changes Flash uses, or the way it writes the boundary. Anyway it would be great if you could upload a file with the response body generated as described above and also please mention your versions for: commons-fileupload, commons-io, Flash player, Browser you are using for your scenario.</p> <hr> <p><strong>Update 2:</strong> Unfortunately I was not able to reproduce your situation using the request's body from the file you provided. While it seems that Flash does not create a strictly valid request (it does not add an extra <code>\r\n</code> at the end of the file) I could upload this request normally with both commons-file-upload 1.2 and 1.3 so I have run out of ideas. </p> <p>The last thing that I suggest is to try to upload a file from a browser using the simple html page below. Put this file in the root of your web application and upload your file <code>Cookie.txt</code> (make sure the <code>action</code> in the form points to the upload servlet). If this does not work then the problem lies on the commons-file-upload side though I can't imagine what it might be. Keeping an eye in the server logs (perhaps an exception is shallowed somewhere?) can't hurt either.</p> <p>The only safe bet that would reveal your problem, is to download the sources of commons-file-upload, put it in your project instead of using it as a dependency and debug. Since the problem occurs every time in your case, it shouldn't be too difficult to find it.</p> <p>Upload html:</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;form action="/scm/uploadServlet" method="post" enctype="multipart/form-data"&gt; &lt;label for="file"&gt;Filename:&lt;/label&gt;&lt;br/&gt; &lt;input type="file" name="myfile" id="file"&gt;&lt;br/&gt; &lt;input type="submit" name="submit" value="Submit"&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </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