• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Jetty在多部分表单请求上抛出“多部分请求的缺少内容”(Jetty throws “Missing conte ...

原作者: [db:作者] 来自: 网络 收藏 邀请

Jetty在多部分表单请求上抛出“多部分请求的缺少内容”(Jetty throws “Missing content for multipart request” on multipart form request)

我正在使用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--

有任何想法吗?


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.

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>

It implements a POST command that should receive a file upload through HttpServletRequest. But when request.getParts() is called, this exception occurs:

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.

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?


原文:https://stackoverflow.com/questions/20613104
更新时间:2022-03-21 16:03

最满意答案

我们遇到了完全相同的问题,经过几个小时的调试后我们发现了问题。

Jetty抱怨因为包含mulitpart消息的InputStream为空。 对我们来说问题是两个Servlet 3.0样式的多部分都已配置(正如你在web.xml使用multipart-*标签),并且Spring被配置为使用Commons FileUpload进行多部分解析。 这导致Commons FileUpload在Jetty在MultiPartInputStreamParser中执行之前使用InputStream

解决这个问题的方法就是从类路径中删除Commons FileUpload,并将Spring配置为不使用它。


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

The solution to this is simply to remove Commons FileUpload from the classpath and configure Spring not to use it.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap