使HttpServletRequest.getParts()与jersey一起使用 [英] Getting HttpServletRequest.getParts() to work with jersey

查看:138
本文介绍了使HttpServletRequest.getParts()与jersey一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

@MultipartConfig(location="/tmp", fileSizeThreshold=1048576,
        maxFileSize=20848820, maxRequestSize=418018841)
@Path("/helloworld")
public class HelloWorld extends HttpServlet {

@POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    //@Consumes()
    @Produces("text/plain")
    public void doPost(@Context HttpServletRequest httpRequest) {
        System.out.println("pinged");
       //...
    }
}

,我想访问零件并获取文件.但是当我执行httpRequest.getPart("token")时,我得到了java.lang.IllegalStateException: Request.getPart is called without multipart configuration.如何使它正常工作?我正在使用Jersey,我知道用FormDataMultiPart可以有更好的方法,但是我的目标是编写一个使用HttpServletRequest并提取一些数据并将其转换为自定义对象的函数. (这里使用jersey服务器纯粹是随机的.我希望我的功能可以与其他没有FormDataMultiPart但有HttpServletRequest的java服务器一起使用.)

and I want to access the parts and get the files. But when I do httpRequest.getPart("token") I get java.lang.IllegalStateException: Request.getPart is called without multipart configuration. How do i get this to work? I am using Jersey and I know there is a better way to do this with FormDataMultiPart but my goal is to write a function that takes a HttpServletRequest and extracts some data and turns it into a custom object. (The use of a jersey server here is purely random. I want my function to work with other java servers that my not have FormDataMultiPart but do have HttpServletRequest).

推荐答案

首先,这不是应该使用JAX-RS的方式.请勿将JAX-RS批注与Servlet混合使用.您需要做的是将多部分配置添加到web.xml中.

First of all this is not how JAX-RS is supposed to be used. You don't mix the JAX-RS annotations with a Servlet. What you need to do is add the multipart config into the web.xml.

<servlet>
    <servlet-name>com.example.AppConfig</servlet-name>
    <load-on-startup>1</load-on-startup>
    <multipart-config>
        <max-file-size>10485760</max-file-size>
        <max-request-size>20971520</max-request-size>
        <file-size-threshold>5242880</file-size-threshold>
    </multipart-config>
</servlet>
<servlet-mapping>
    <servlet-name>com.example.AppConfig</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

请注意,servlet名称是Application子类的完全限定类.

Notice the servlet-name is the fully qualified class of the Application subclass.

其余的我曾经测试过的

import javax.ws.rs.core.Application;

// if you're using `@ApplicationPath`, remove it
public class AppConfig extends Application {

}

@Path("upload")
public class FileUpload {
    @POST
    @Path("servlet")
    public Response upload(@Context HttpServletRequest request)
            throws IOException, ServletException {
        Collection<Part> parts = request.getParts();
        StringBuilder sb = new StringBuilder();
        for (Part part: parts) {
            sb.append(part.getName()).append("\n");
        }
        return Response.ok(sb.toString()).build();
    }
}

这篇关于使HttpServletRequest.getParts()与jersey一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆