Multipart formdata not working with Jersey and JaxRs

那年仲夏 提交于 2020-01-16 19:09:49

问题


I want to build support for fileupload using Jersey (2.22.2) & JaxRs.

This is my application:

@ApplicationPath("rest")
public class DsmJaxRsApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<>();

        classes.add(FileUploadResource.class);
        classes.add(MultiPartFeature.class);

        return classes;
    }
}

This is my resource method to intercept the POST request:

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
@Path("upload")
public Response uploadImportFile(@FormDataParam("reason") String reason,
                                 @FormDataParam("fileName") String fileName,
                                 @FormDataParam("file") InputStream fileContent) {
    checkCreateBulkChangeAllowed();

    return Response.ok().build();
}

And this is my request payload:

------WebKitFormBoundaryAh9J98cKgsOv6WCr
Content-Disposition: form-data; name="reason"

wwwww
------WebKitFormBoundaryAh9J98cKgsOv6WCr
Content-Disposition: form-data; name="fileName"

DSM_CH_Bulk_new.xlsx
------WebKitFormBoundaryAh9J98cKgsOv6WCr
Content-Disposition: form-data; name="file"; filename="DSM_CH_Bulk_new.xlsx"
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet


------WebKitFormBoundaryAh9J98cKgsOv6WCr--

The problem is that inside the method both the fileContent and the reason contain "wwww" which should be the reason and the fileName is always null. Does anyone have any idea what am I doing wrong or what am I missing here?


回答1:


My configuration was based in a web.xml file. Try to add FormDataContentDisposition

@POST
@Path("upload/file")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response uploadInspectionFile(@FormDataParam("file") InputStream inputStream,
        @FormDataParam("file") FormDataContentDisposition content,
        @FormDataParam("path") String path)

UPDATE

Here is my servlet in my web.xml file:

<servlet>
    <servlet-name>servlet_inspector</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>io.swagger.jaxrs.listing,com.sagasoftware.service;com.sagasoftware.custom.filter</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>
            org.glassfish.jersey.media.multipart.MultiPartFeature ,
            io.swagger.jaxrs.listing.ApiListingResource,
            io.swagger.jaxrs.listing.SwaggerSerializers,
    </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

and here are my dependencies:

<properties>
    <jersey.version>2.22.1</jersey.version>
    <jackson.version>2.6.1</jackson.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<version>1</version>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.0.4.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>5.0.7.Final</version>
    </dependency>

    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.9</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.39</version>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.4</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>

    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.5.0-b01</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
    </dependency>

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>

    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.2.2</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version>
    </dependency>

    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-jersey2-jaxrs</artifactId>
        <version>1.5.9</version>
    </dependency>

    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>1.5.9</version>
    </dependency>

</dependencies>


来源:https://stackoverflow.com/questions/38361880/multipart-formdata-not-working-with-jersey-and-jaxrs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!