使用Jersey multipart扩展程序上传文件会返回badrequest

时间:2015-12-20 10:43:21

标签: java jersey-2.0

我正在尝试将文件上传到具有多部分功能的泽西服务,但我不断收到400错误请求错误。我真的很感激任何帮助。

到目前为止:

  • 我尝试将依赖版本升级到最新版本。

  • 从服务方法签名中删除FormDataParam注入修复了错误的请求错误,但使其无效。

以下是我的依赖项和代码:

上传方法签名:

  @POST
@Path("/uploadMe")
@Consumes(MediaType.MULTIPART_FORM_DATA +";charset=utf-8")
@Produces(MediaType.APPLICATION_JSON +";charset=utf-8")
public Response UploadMe(@FormDataParam("file")InputStream fileInputStream,
                                     @FormDataParam("file")FormDataContentDisposition fileDetail
                                       )
{}

自举:

   public static void main( String[] args ) throws Exception {
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");

    Server jettyServer = new Server(8080);
    jettyServer.setHandler(context);

    ServletHolder jerseyServlet = context.addServlet(
            org.glassfish.jersey.servlet.ServletContainer.class, "/*");
    jerseyServlet.setInitOrder(0);


    jerseyServlet.setInitParameter(
            "jersey.config.server.provider.classnames",
            MyService.class.getCanonicalName()+";"+
                    LoggingFilter.class.getCanonicalName() + ";" +
                    MultiPartFeature.class.getCanonicalName());

    try {
        jettyServer.start();
        jettyServer.join();
    } catch(Throwable e){
        e.printStackTrace();
    }finally {
        jettyServer.destroy();
    }
}

依赖关系:

 <dependencies>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-server</artifactId>
  <version>9.2.3.v20140905</version>
</dependency>
<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-servlet</artifactId>
  <version>9.2.3.v20140905</version>
</dependency>
<dependency>
  <groupId>org.glassfish.jersey.core</groupId>
  <artifactId>jersey-server</artifactId>
  <version>2.7</version>
</dependency>
<dependency>
  <groupId>org.glassfish.jersey.containers</groupId>
  <artifactId>jersey-container-servlet-core</artifactId>
  <version>2.7</version>
</dependency>
<dependency>
  <groupId>org.glassfish.jersey.containers</groupId>
  <artifactId>jersey-container-jetty-http</artifactId>
  <version>2.7</version>
</dependency>
<dependency>
  <groupId>org.glassfish.jersey.media</groupId>
  <artifactId>jersey-media-moxy</artifactId>
  <version>2.7</version>
</dependency>

<dependency>
  <groupId>org.glassfish.jersey.media</groupId>
  <artifactId>jersey-media-multipart</artifactId>
  <version>2.7</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>4.2.3.Final</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-annotations</artifactId>
  <version>3.5.6-Final</version>
</dependency>
<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>9.4-1201-jdbc41</version>
</dependency>
<dependency>
  <groupId>net.sf.ehcache</groupId>
  <artifactId>ehcache</artifactId>
  <version>2.7.2</version>
</dependency>
<dependency>
  <groupId>net.sf.ehcache</groupId>
  <artifactId>ehcache-core</artifactId>
  <version>2.4.3</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-ehcache</artifactId>
  <version>4.2.3.Final</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.5</version>
</dependency>
<dependency>
  <groupId>commons-validator</groupId>
  <artifactId>commons-validator</artifactId>
  <version>1.4.1</version>
</dependency>
<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-mapper-asl</artifactId>
  <version>1.9.13</version>
</dependency>
<dependency>
  <groupId>javax.mail</groupId>
  <artifactId>mail</artifactId>
  <version>1.4.7</version>
</dependency>

原始请求:

POST http://localhost:8080/service/uploadMe HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 254
Cache-Control: no-cache
Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
user: xxxxxx
Content-Type: multipart/form-data
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Postman-Token: 9ad2fb80-3b97-2a13-568f-5705d9f603a4
Accept: */*
Accept-Encoding: gzip, deflate

------WebKitFormBoundaryFBm4Vxg6YCzF57Fb
Content-Disposition: form-data; name="file"; filename="sampledatax.txt"
Content-Type: text/plain

asdasdasdasdasdad;test,test1
dfdshjkfasldhflsahdfsadf;test,test2
------WebKitFormBoundaryFBm4Vxg6YCzF57Fb--

0 个答案:

没有答案