简体   繁体   中英

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

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.

自从我处理该库以来已经过去了大约一年,但如果我没记错,如果有人试图上传文件,然后更改浏览器URL(点击链接,打开书签等),那么你就可以得到那个例外。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM