Why did I get “FileUploadException: Stream ended u

2019-01-26 09:47发布

问题:

What is the reason for encountering this Exception:

org.apache.commons.fileupload.FileUploadException: 
  Processing of multipart/form-data request failed. Stream ended unexpectedly

回答1:

The main reason is that the underlying socket was closed or reset. The most common reason is that the user closed the browser before the file was fully uploaded. Or the Internet was interrupted during the upload. In any case, the server side code should be able to handle this exception gracefully.



回答2:

Its been about a year since I dealt with that library, but if I remember correctly, if someone tries to upload a file, then changes the browser URL (clicks a link, opens a bookmark, etc) then you could get that exception.



回答3:

You could possibly get this exception if you're using FileUpload to receive an upload from flash.

At least as of version 8, Flash contains a known bug: The multipart stream it produces is broken, because the final boundary doesn't contain the suffix "--", which ought to indicate, that no more items are following. Consequently, FileUpload waits for the next item (which it doesn't get) and throws an exception.

There is a workaround suggests to use the streaming API and catch the exception.

catch (MalformedStreamException e) {
    // Ignore this
}

For more details, please refer to https://commons.apache.org/proper/commons-fileupload/faq.html#missing-boundary-terminator