13

What is the reason for encountering this Exception:

org.apache.commons.fileupload.FileUploadException: 
  Processing of multipart/form-data request failed. Stream ended unexpectedly
Eddie
  • 53,828
  • 22
  • 125
  • 145

3 Answers3

12

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.

Tommy Hui
  • 1,306
  • 6
  • 9
  • In the first case, you can put some validation in the client side to avoid files too large to upload. And yes, sure, some browsers do not allow to check the file before upload. IE9 and the older ones are the classic examples. – victorf May 20 '16 at 18:41
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

3

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.

CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138