java 损坏的表单数据:过早结束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/240163/
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
Corrupt form data: premature ending
提问by Matt MacLean
I am trying to upload files using the FileReference class. Files >2MB all work correctly but files <2MB cause this error:
我正在尝试使用 FileReference 类上传文件。大于 2MB 的文件都可以正常工作,但小于 2MB 的文件会导致此错误:
"java.io.IOException: Corrupt form data: premature ending"
“java.io.IOException:损坏的表单数据:过早结束”
On the server I am using the com.oreilly.servlet package to handle the request.
在服务器上,我使用 com.oreilly.servlet 包来处理请求。
I have used this package many times to successfully handle file uploads from flex, but for some reason, now I am having this problem.
我已经多次使用这个包来成功处理从 flex 上传的文件,但由于某种原因,现在我遇到了这个问题。
Here is the stack trace for some more info:
这是有关更多信息的堆栈跟踪:
java.io.IOException: Corrupt form data: premature ending
at com.oreilly.servlet.multipart.MultipartParser.<init>(MultipartParser.java:205)
at com.oreilly.servlet.MultipartRequest.<init>(MultipartRequest.java:222)
at com.oreilly.servlet.MultipartRequest.<init>(MultipartRequest.java:173)
at com.mydomain.FileUploadServlet.doPost(FileUploadServlet.java:46)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:414)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
采纳答案by Matt MacLean
It seems that there is a bug that exists when using com.orielly.servlet.MultipartRequest class and the org.apache.struts2.dispatcher.ActionContextCleanUp filter together. This is what was causing small file uploads to fail.
似乎在使用 com.orielly.servlet.MultipartRequest 类和 org.apache.struts2.dispatcher.ActionContextCleanUp 过滤器时存在一个错误。这就是导致小文件上传失败的原因。
回答by Vladimir Dyuzhev
http://www.servlets.com/cos/faq.html
http://www.servlets.com/cos/faq.html
Why when using com.oreilly.servlet.MultipartRequest or MultipartParser do large uploads fail?The classes themselves were specifically designed to have no maximum upload size limit (unlike most other file upload utilities), but for your server's protection the constructor allows you to set a maximum POST size to accept. Any upload larger than the limit is halted. The default maximum is 1 Meg. For a discussion of the difficulties a server has in notifying a client of the error, see the discussion in Java Servlet Programming, 2nd Edition, page 119.
为什么在使用 com.oreilly.servlet.MultipartRequest 或 MultipartParser 时大上传失败?这些类本身专门设计为没有最大上传大小限制(与大多数其他文件上传实用程序不同),但为了保护您的服务器,构造函数允许您设置接受的最大 POST 大小。任何大于限制的上传都将停止。默认最大值为 1 兆。有关服务器在将错误通知客户端时遇到的困难的讨论,请参阅 Java Servlet Programming, 2nd Edition, page 119 中的讨论。
So, did you specify the maximum POST size to accept?
那么,您是否指定了要接受的最大 POST 大小?
P.S. Ok, now I see that it's small uploads that cause the problem. On the FAQ link above there is a section dedicated to troubleshooting uploads, including some helful methods to isolate the cause (client, browser, web-server, libraries). Try them.
PS 好的,现在我看到是小上传导致了问题。在上面的常见问题链接上,有一个部分专门用于解决上传问题,包括一些有用的方法来隔离原因(客户端、浏览器、Web 服务器、库)。试试他们。
Install a Firefox plugin (Tamper Data or Firebug) that shows the request sent to server. May help you to understand if anything is different between <2M and >2M uploads.
安装显示发送到服务器的请求的 Firefox 插件(Tamper Data 或 Firebug)。可以帮助您了解 <2M 和 >2M 上传之间是否有任何不同。
P.P.S. Are the files of the same structure? Could it be that smaller ones have different data (e.g. special symbols) that break Flash library? Try to upload small files of spaces only, for instance.
PPS 是相同结构的文件吗?会不会是较小的具有破坏 Flash 库的不同数据(例如特殊符号)?例如,尝试仅上传空间的小文件。
回答by Matt MacLean
@Vladimir:
@弗拉基米尔:
I have used an http sniffer to check the post request and it is sending the entire file and the post request format is correct. I have tried many different files (.jpg, .mp3 etc) that are <2MB and none are working.
我使用了一个 http 嗅探器来检查 post 请求,它正在发送整个文件并且 post 请求格式是正确的。我尝试了许多小于 2MB 的不同文件(.jpg、.mp3 等),但都没有工作。
The maximum post size is set to 1.5GB.
最大帖子大小设置为 1.5GB。
Here is what the post request is:
这是post请求的内容:
------------cH2ae0ei4ae0cH2ae0Ef1KM7gL6GI3
Content-Disposition: form-data; name="Filename"
IMG0001.jpg
------------cH2ae0ei4ae0cH2ae0Ef1KM7gL6GI3
Content-Disposition: form-data; name="Filedata"; filename="IMG0001.jpg"
Content-Type: application/octet-stream
<file data here>
------------cH2ae0ei4ae0cH2ae0Ef1KM7gL6GI3
Content-Disposition: form-data; name="Upload"
Submit Query
------------cH2ae0ei4ae0cH2ae0Ef1KM7gL6GI3--
I have confirmed that this is a server side issue as I tested the servlet with a regular html form and I get the same results.
我已经确认这是服务器端问题,因为我使用常规 html 表单测试了 servlet,并且得到了相同的结果。
Any other ideas?
还有其他想法吗?
回答by Matt MacLean
The reason of this is because the request is been sent with a "Transfer Encoding: chunked" header, instead of a Content-length header. Many servers don't understand chunked content, and neither does o'reilly. Check with a sniffer if your request is been send with the Transfer encoding header. I don't know any solution for this.
这样做的原因是因为请求是使用“传输编码:分块”标头而不是内容长度标头发送的。许多服务器不理解分块内容,o'reilly 也不理解。使用嗅探器检查您的请求是否与传输编码标头一起发送。我不知道有什么解决方案。
回答by diaaa
If you are not using structs then it can be solved by deleting the structs libraries from your project's library .
如果您不使用结构体,则可以通过从项目库中删除结构体库来解决。

