Java Jetty 在多部分表单请求上抛出“多部分请求缺少内容”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20613104/
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
Jetty throws "Missing content for multipart request" on multipart form request
提问by Kof
I'm running Jetty 9 (jetty-9.0.5.v20130815) server with servlet-api 3.0.1 (javax.servlet:javax.servlet-api:3.0.1), my servlet is configured to accept multipart requests.
我正在运行带有 servlet-api 3.0.1 (javax.servlet:javax.servlet-api:3.0.1) 的 Jetty 9 (jetty-9.0.5.v20130815) 服务器,我的 servlet 被配置为接受多部分请求。
web.xml -
web.xml -
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:fmt="http://java.sun.com/jsp/jstl/fmt"
xmlns:web="http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee">
<servlet>
<servlet-name>restapi</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
<multipart-form enable="true" />
<multipart-config>
<max-file-size>8428800</max-file-size>
<max-request-size>8428800</max-request-size>
<file-size-threshold>1048576</file-size-threshold>
</multipart-config>
</servlet>
</web-app>
applicationContext.xml -
applicationContext.xml -
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="8000000"/>
</bean>
It implements a POST command that should receive a file upload through HttpServletRequest
. But when request.getParts()
is called, this exception occurs:
它实现了一个 POST 命令,该命令应该通过HttpServletRequest
. 但是当request.getParts()
被调用时,会出现这个异常:
java.io.IOException: Missing content for multipart request
at org.eclipse.jetty.util.MultiPartInputStreamParser.parse(MultiPartInputStreamParser.java:493)
at org.eclipse.jetty.util.MultiPartInputStreamParser.getParts(MultiPartInputStreamParser.java:401)
at org.eclipse.jetty.server.Request.getParts(Request.java:2077)
at javax.servlet.http.HttpServletRequestWrapper.getParts(HttpServletRequestWrapper.java:351)
at javax.servlet.http.HttpServletRequestWrapper.getParts(HttpServletRequestWrapper.java:351)
The client is an iOS app using ASIFormDataRequest, but I don't think that client has the problem because it used to work with Apache commons-fileupload.jar. I'm not switching the server to use Jetty's multipart support for receiving files.
客户端是一个使用 ASIFormDataRequest 的 iOS 应用程序,但我认为客户端没有问题,因为它曾经与 Apache commons-fileupload.jar 一起使用。我不会切换服务器以使用 Jetty 的多部分支持来接收文件。
Here's the request, as intercepted by a proxy -
这是代理拦截的请求 -
POST /mycommand HTTP/1.1
Host: localhost:8080
Proxy-Connection: close
Accept-Encoding: gzip
Content-Type: multipart/form-data; charset=utf-8; boundary=0xKhTmLbOuNdArY-849F4DA3-85DC-46F1-B182-44257808F0B9
Content-Length: 1568
Connection: close
--0xKhTmLbOuNdArY-849F4DA3-85DC-46F1-B182-44257808F0B9
Content-Disposition: form-data; name="param1"
text
--0xKhTmLbOuNdArY-849F4DA3-85DC-46F1-B182-44257808F0B9
Content-Disposition: form-data; name="data"; filename="file"
Content-Type: application/octet-stream
... DATA ...
--0xKhTmLbOuNdArY-849F4DA3-85DC-46F1-B182-44257808F0B9--
Any ideas?
有任何想法吗?
采纳答案by Aleksander Blomsk?ld
We had the exact same problem, and after some hours of debugging we found the issue.
我们遇到了完全相同的问题,经过几个小时的调试后,我们发现了问题。
Jetty complains because the InputStream that contains the mulitpart message is empty. The issue for us was that both both Servlet 3.0-style multipart was configured (as you have done with the multipart-*
tags in web.xml
), and Spring was configured to use Commons FileUpload for multipart parsing. This caused Commons FileUpload to consume the InputStream before Jetty did it in the MultiPartInputStreamParser
Jetty 抱怨是因为包含多部分消息的 InputStream 是空的。对我们而言,问题是 Servlet 3.0 样式的多部分都已配置(正如您对 中的multipart-*
标记所做的那样web.xml
),并且 Spring 被配置为使用 Commons FileUpload 进行多部分解析。这导致 Commons FileUpload 在 Jetty 在MultiPartInputStreamParser
The solution to this is simply to remove Commons FileUpload from the classpath and configure Spring not to use it.
解决这个问题的方法是简单地从类路径中删除 Commons FileUpload 并配置 Spring 不使用它。
回答by Joakim Erdfelt
The error for "Missing content for multipart request"
is due to a missing or badly detected first boundary.
错误"Missing content for multipart request"
是由于第一边界缺失或检测错误造成的。
There have been numerous edge cases (from clients that badly format multi-part messages) that Jetty compensates for. However, the information you have provided is insufficient to know what the underlying cause might be.
Jetty 补偿了许多边缘情况(来自对多部分消息格式错误的客户端)。但是,您提供的信息不足以了解根本原因可能是什么。
Can you file a bug at bugs.eclipse.org under RT/Jettywith a wireshark capture of the traffic?
您可以在 RT/Jetty 下的 bugs.eclipse.org 上提交错误,并使用 wireshark 捕获流量吗?
回答by z0mb1ek
for spring boot application I found same issue. I remove Commons FileUpload lib and add to properties:
对于 Spring Boot 应用程序,我发现了同样的问题。我删除了 Commons FileUpload 库并添加到属性中:
multipart:
maxFileSize: 32Mb
maxRequestSize: 32Mb
for size files
对于大小文件
回答by shareef
In my case i had the following ; when i commented the lines below in
就我而言,我有以下内容;当我评论下面的行时
org.springframework.web.servlet.DispatcherServlet
org.springframework.web.servlet.DispatcherServlet
It worked.....after commenting
它起作用了.....评论后
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<multipart-config>
<!-- 52MB max -->
<max-file-size>52428800</max-file-size>
<max-request-size>52428800</max-request-size>
<file-size-threshold>0</file-size-threshold>
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.ico</url-pattern>
<url-pattern>*.png</url-pattern>
<url-pattern>*.jpg</url-pattern>
<url-pattern>*.htc</url-pattern>
<url-pattern>*.gif</url-pattern>
<url-pattern>*.swf</url-pattern>
<url-pattern>*.wmf</url-pattern>
<url-pattern>*.pdf</url-pattern>
<url-pattern>*.css</url-pattern>
<url-pattern>*.js</url-pattern>
<url-pattern>*.json</url-pattern>
<url-pattern>*.otf</url-pattern>
<url-pattern>*.eot</url-pattern>
<url-pattern>*.svg</url-pattern>
<url-pattern>*.ttf</url-pattern>
<url-pattern>*.woff</url-pattern>
<url-pattern>/files/original/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
<!-- <multipart-config>
52MB max
<max-file-size>52428800</max-file-size>
<max-request-size>52428800</max-request-size>
<file-size-threshold>0</file-size-threshold>
</multipart-config>-->
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>*.txt</url-pattern>
<url-pattern>*.xml</url-pattern>
<url-pattern>/</url-pattern>
</servlet-mapping>