我正在使用servlet-api 3.0.1(javax.servlet:javax.servlet-api:3.0.1)运行Jetty 9(jetty-9.0.5.v20130815)服务器,我的servlet配置为接受多部分请求.
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-
<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>
它实现了一个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)
该客户端是使用ASIFormDataRequest的iOS应用,但我认为该客户端没有问题,因为它曾经与Apache commons-fileupload.jar一起使用.我不会将服务器切换为使用Jetty的多部分支持来接收文件.
这是代理拦截的请求-
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--
有任何想法吗?
解决方法:
我们遇到了完全相同的问题,经过几个小时的调试,我们发现了问题.
Jetty抱怨,因为包含mulitpart消息的InputStream为空.对我们来说,问题在于,这两个Servlet 3.0样式的多部分都已配置(就像您对web.xml中的multipart- *标记所做的那样),并且Spring已配置为使用Commons FileUpload进行多部分解析.这导致Commons FileUpload在Jetty在MultiPartInputStreamParser中完成之前消耗了InputStream.
解决方案只是从类路径中删除Commons FileUpload并配置Spring不使用它.