1

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

代码:

    @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

谢谢!

4

2 回答 2

2

如果你使用 jersey 作为 Servlet,你可以在 web.xml 中设置 multipart 参数

<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

于 2013-05-04T08:26:38.417 回答
1

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

于 2012-08-20T15:16:40.107 回答