为什么jaxrs multipart请求抛出IllegalStateException?

时间:2012-08-18 07:39:55

标签: java-ee jax-rs

为什么此代码会返回此错误?

代码:

    @Path("/site/resource/upload")
    @Local
    @Stateless
    public class TreeResourceResourcesUpload extends TreeBaseResources {

    @POST
    @Produces( MediaType.APPLICATION_JSON )
    @Consumes( MediaType.MULTIPART_FORM_DATA )
    public TreeNodeTO uploadResource(@Context HttpServletRequest request) throws IOException, ServletException {
            for (Part part : request.getParts()) {
                System.out.println(part.getName());
            }
            return null;
    }
}

错误:

Caused by: java.lang.IllegalStateException: PWC4016: Request.getParts is called without multipart configuration.  Either add a @MultipartConfig to the servlet, or a multipart-config element to web.xml

系统: Glassfish 3.1.2.2和Sun JDK 7

谢谢!

2 个答案:

答案 0 :(得分:2)

如果您使用jersey作为Servlet,则可以在web.xml中设置多部分参数

<servlet>
    <servlet-name>jersey-servlet</servlet-name>
    <servlet-class>
        com.sun.jersey.spi.container.servlet.ServletContainer
    </servlet-class>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
        <multipart-config>
            <location>/tmp</location>
            <max-file-size>20848820</max-file-size>
            <max-request-size>418018841</max-request-size>
            <file-size-threshold>1048576</file-size-threshold>
        </multipart-config>
</servlet>
<servlet-mapping>
    <servlet-name>jersey-servlet</servlet-name>
    <url-pattern>/rs/*</url-pattern>
</servlet-mapping>

但我认为这种方法不符合标准。 我正在寻找JAXRS规范中的答案,什么都没有。 所以我认为在JAX-RS中无法访问.getPart

答案 1 :(得分:1)

我似乎遇到了类似的问题。它看起来像bug http://java.net/jira/browse/GLASSFISH-18444,应该在3.1.2.2中修复,但是不是吗?