Dick Song
Dick Song

Reputation:

Why did I get "FileUploadException: Stream ended unexpectedly" with Apache Commons FileUpload?

What is the reason for encountering this Exception:

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

Upvotes: 13

Views: 47466

Answers (3)

Jun Yeong Lee
Jun Yeong Lee

Reputation: 41

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

Upvotes: 3

CodingWithSpike
CodingWithSpike

Reputation: 43708

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.

Upvotes: 3

Tommy Hui
Tommy Hui

Reputation: 1336

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.

Upvotes: 12

Related Questions