java IE 11:发送多部分表单数据请求时出错:流意外结束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27903414/
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
IE 11: Error while sending Multipart Form Data request: Stream ended unexpectedly
提问by Pramod Karandikar
I am trying to upload files along with some other form fields using jQuery AJAX calls.
我正在尝试使用 jQuery AJAX 调用上传文件以及其他一些表单字段。
This is a common function that calls the URL on the server:
这是调用服务器上的 URL 的常用函数:
function uploadDocument(rquestURL,formId,callback){
$.ajax({
type : 'POST',
url : rquestURL,
cache:false,
processData:false,
contentType:false,
data : new FormData($("#"+formId)[0])
}).done(function(response) {
callback(response);
});
}
On examining from the dev tools from browsers, these are the respective request contents:
从浏览器的开发工具中查看,这些是各自的请求内容:
From IE11
从 IE11
-----------------------------7dfad39402e6
Content-Disposition: form-data; name="subject"
Test
-----------------------------7dfad39402e6
Content-Disposition: form-data; name="message"
Test test
-----------------------------7dfad39402e6
Content-Disposition: form-data; name="announcementAttachment"; filename=""
Content-Type: application/octet-stream
<Binary File Data Not Shown>
---------------------------7dfad39402e6
Chrome
铬合金
------WebKitFormBoundaryp8rj3ArKDsbYw0BZ
Content-Disposition: form-data; name="subject"
Test
------WebKitFormBoundaryp8rj3ArKDsbYw0BZ
Content-Disposition: form-data; name="message"
Test test
------WebKitFormBoundaryp8rj3ArKDsbYw0BZ
Content-Disposition: form-data; name="announcementAttachment"; filename=""
Content-Type: application/octet-stream
------WebKitFormBoundaryp8rj3ArKDsbYw0BZ--
On server side, we are parsing the request as:
在服务器端,我们将请求解析为:
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
final FileItemFactory factory = new DiskFileItemFactory();
final ServletFileUpload fileUpload = new ServletFileUpload(factory);
if (ServletFileUpload.isMultipartContent(request)) {
// get the request content and iterate through
items = fileUpload.parseRequest(request);
}
The code works fine from Chrome and Firefox, but throws the below exception when I tried from IE11.
该代码在 Chrome 和 Firefox 中运行良好,但当我从 IE11 尝试时抛出以下异常。
javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: org.apache.commons.fileupload.FileUploadException: Stream ended unexpectedly
I referred to these SO questions but in vain.
我提到了这些 SO 问题,但徒劳无功。
- Java - FormData in IE throwing org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
- Grails Multipart Service Stream ended unexpectedly
- Java - IE 中的 FormData 抛出 org.apache.commons.fileupload.MultipartStream$MalformedStreamException:流意外结束
- Grails 多部分服务流意外结束
Any useful pointers are appreciated. Thanks.
任何有用的指针表示赞赏。谢谢。
回答by Pramod Karandikar
Turned out a weird issue. This is how it's resolved.
发现了一个奇怪的问题。就是这样解决的。
- We had checkboxes at the end of the form. The mentioned issue was occurring when we do not select any of the checkboxes. The request was not getting formed correctly and hence server threw error.
- Added a hidden field at the end of the form (make sure this is the last form field) and assigned some value to it.
- 我们在表单末尾有复选框。当我们不选择任何复选框时,就会发生上述问题。请求未正确形成,因此服务器抛出错误。
- 在表单末尾添加了一个隐藏字段(确保这是最后一个表单字段)并为其分配一些值。
That' it. Worked like a magic!
就是这样。像魔术一样工作!
More info here.
更多信息在这里。
回答by Venkatesh Varadharajan
I had the same problem. I was having only the id attribute and missing the name attribute in the hidden input field which gave me the below error. Issue resolved after adding the name attribute to the input hidden type field.
我有同样的问题。我只有 id 属性,而在隐藏输入字段中缺少 name 属性,这给了我以下错误。将名称属性添加到输入隐藏类型字段后问题已解决。
id="timestamp" name="timestamp"
id="时间戳" 名称="时间戳"
Caused by: org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly Caused by: org.apache.commons.fileupload.FileUploadException: Stream ended unexpectedly
引起:org.apache.commons.fileupload.MultipartStream$MalformedStreamException:流意外结束引起:org.apache.commons.fileupload.FileUploadException:流意外结束
回答by sanjeev sobhun
It happened to me, the problem was that there was location.reload once document was selected for upload. This stopped the stream to be parsed.
它发生在我身上,问题是一旦选择了要上传的文档,就会出现 location.reload。这停止了要解析的流。
回答by shaangon
The issue you have mentioned has troubled me for a long time. I finally have a solution to the issue. IE appends an empty name form data at the end of the formData request object which gets parsed at the server and hence the error occurs.
你提到的问题困扰了我很久。我终于有办法解决这个问题了。IE 在 formData 请求对象的末尾附加一个空名称表单数据,该对象在服务器上被解析,因此发生错误。
Below was the form data request object which was sent before the fix:
下面是在修复之前发送的表单数据请求对象:
-----------------------------7e3195134f056c
Content-Disposition: form-data; name="csrfToken"
8394D82F5A776708F13CDC6D4B4DE1485C1EC05625E63B2E
-----------------------------7e3195134f056c
Content-Disposition: form-data; name="ACTION"
DELETE_LOGO
-----------------------------7e3195134f056c
Content-Disposition: form-data; name="ORG_ID"
1879048492
-----------------------------7e3195134f056c
Content-Disposition: form-data; name="
-----------------------------7e3195134f056c--
In order to resolve the issue, added an extra hidden field at the end of the form element.
为了解决这个问题,在表单元素的末尾添加了一个额外的隐藏字段。
<csrf:form name="OrgLogoEdit" METHOD="POST" ACTION="/logo" onKeyDown="" enctype="multipart/form-data" accept-charset="UTF-8">
<INPUT TYPE = HIDDEN NAME = "<%= Control._ACTION %>" VALUE = "<%= OrganizationLogo._UPLOAD_LOGO %>">
<INPUT TYPE = HIDDEN NAME = "<%= Control.ORG_ID %>" VALUE = "<%= organization.getId() %>">
<div class="cropit-preview"></div>
<input type="range" min="0" max="100" class="cropit-image-zoom-input" step="any">
<input type="hidden" name="dummyIEField"> <!-- this dummy hidden field resolves the stream ended unexpectedly issue -->
</csrf:form>
The request body is now sent as below which is parsed successfully:
请求正文现在发送如下,成功解析:
-----------------------------7e3195134f056c
Content-Disposition: form-data; name="csrfToken"
8394D82F5A776708F13CDC6D4B4DE1485C1EC05625E63B2E
-----------------------------7e3195134f056c
Content-Disposition: form-data; name="ACTION"
DELETE_LOGO
-----------------------------7e3195134f056c
Content-Disposition: form-data; name="ORG_ID"
1879048492
-----------------------------7e3195134f056c
Content-Disposition: form-data; name="dummyIEField"
-----------------------------7e3195134f056c--
Hope this helps. Cheers !!
希望这可以帮助。干杯!!