The answer is a little late, but I had the same problem.
Why you get this exception: JavaDocs of ItemSkippedException explains a bit:
This exception is thrown if an attempt is made to read data from an InputStream that was returned by FileItemStream.openStream () after Iterator.hasNext () was called on the iterator that created FileItemStream.
You are using an InputStream outside of the while loop, which causes a problem because another iteration is called that closes (skips) the InputStream file that you are trying to read.
Solution: use an InputStream inside a while loop. If you need all the form fields before processing the file, make sure that you install it in the correct order on the client side. First all the fields, the last file. For example, using the JavaScript form FormData:
var fd = new window.FormData(); fd.append("param1", param1); fd.append("param2", param2);
And on the server side:
FileItemIterator iter = upload.getItemIterator(request); while (iter.hasNext()) { FileItemStream item = iter.next(); InputStream stream = item.openStream(); // the order of items is given by client, first form-fields, last file stream if (item.isFormField()) { String name = item.getFieldName(); String value = Streams.asString(stream); // here we get the param1 and param2 } else { String filename = item.getName(); String mimetype = item.getContentType(); ByteArrayOutputStream output = new ByteArrayOutputStream(); int nRead; while ((nRead = stream.read(buffer, 0, buffer.length)) != -1) { System.out.println("lenth111" +nRead); output.write(buffer, 0, nRead); } System.out.println("lenth" +nRead); output.flush(); } }
chrise
source share