Java 为什么我在使用 Apache Commons FileUpload 时收到“FileUploadException:Stream 意外结束”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/646189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:21:01  来源:igfitidea点击:

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

javafile-uploadapache-commons

提问by

What is the reason for encountering this Exception:

遇到这个异常的原因是什么:

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

回答by Tommy Hui

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.

主要原因是底层套接字已关闭或重置。最常见的原因是用户在文件完全上传之前关闭了浏览器。或者在上传过程中互联网中断了。在任何情况下,服务器端代码都应该能够优雅地处理这个异常。

回答by CodingWithSpike

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.

自从我处理该库以来已经大约一年了,但如果我没记错的话,如果有人尝试上传文件,然后更改浏览器 URL(单击链接、打开书签等),那么您可能会收到该异常。

回答by Jun Yeong Lee

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

如果您使用 FileUpload 从 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.

至少在版本 8 中,Flash 包含一个已知错误:它产生的多部分流被破坏,因为最终边界不包含后缀“--”,这应该表明后面没有更多项目。因此, FileUpload 等待下一项(它没有得到)并抛出异常。

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

有一种解决方法建议使用流 API 并捕获异常。

catch (MalformedStreamException e) {
    // Ignore this
}

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

更多详情请参考https://commons.apache.org/proper/commons-fileupload/faq.html#missing-boundary-terminator