多部分表单数据不适用于Jersey和JaxR [英] Multipart formdata not working with Jersey and JaxRs
问题描述
我想使用Jersey(2.22.2)&来建立对文件上传的支持. JaxRs.
I want to build support for fileupload using Jersey (2.22.2) & 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请求的资源方法:
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--
问题是方法内部的fileContent
和reason
都包含"wwww",这应该是原因,而fileName
始终是null
.有人知道我在做什么错吗或者我在这里想念什么?
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?
推荐答案
我的配置基于web.xml文件.尝试添加 FormDataContentDisposition
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)
更新
这是我的web.xml文件中的servlet:
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>
这是我的依赖项:
<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>
这篇关于多部分表单数据不适用于Jersey和JaxR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!