Multipart formdata无法使用Jersey和JaxR

时间:2016-07-13 21:19:40

标签: java rest jax-rs multipartform-data jersey-2.0

我想使用Jersey(2.22.2)和&amp ;;建立对fileupload的支持。 JAXRS。

这是我的申请:

@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;
    }
}

这是拦截POST请求的资源方法:

@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();
}

这是我的请求有效载荷:

------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--

问题是在方法内部,fileContentreason都包含“wwww”,原因应该是fileNamenull始终是let baseURL = "https://api.instagram.com/v1/tags/nofilter/media/recent?access_token=1234567.d7e73cc.cces4f9532ba442529270d698fb173cc" let popularEndpoint = "media/popular" let clientID = "d5e23bhb23414e569480724305d3f104" 。有谁知道我做错了什么或我在这里错过了什么?

1 个答案:

答案 0 :(得分:0)

我的配置基于web.xml文件。尝试添加 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)

<强>更新

这是我的web.xml文件中的servlet:

<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>

这是我的依赖项:

<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>