Jersey客户端文件上载代码415 - 不支持的媒体类型

时间:2016-03-15 07:46:33

标签: java rest file-upload jersey

我试图创建一个球衣客户端来上传文件。我有一个网络服务和客户端。但我每次都得到415 - 不支持的媒体类型,我不知道问题是什么。 我搜索了i-net / stackoverflow,但我无法找到适合我的问题的解决方案。我尝试了很多解决方案但没有成功。您可以在我的代码中将其视为注释。

客户端:

private static void clientFileUpload() throws IOException {
    ClientConfig config = new ClientConfig();
    Client client       = ClientBuilder.newClient(config);
    client.register(MultiPartFeature.class);

    URI baseUri            = UriBuilder.fromUri("http://localhost:8080/vogella.jersey.first").build();
    final WebTarget target = client.target(baseUri);

    final FileDataBodyPart filepart           = new FileDataBodyPart("files", new File("C:/Users/admin/Downloads/testtext.txt"));
    final FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
    final FormDataMultiPart multiPart         = (FormDataMultiPart) formDataMultiPart.field("foo", "bar").bodyPart(filepart);
    multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
    Response response = target.path("rest").path("hello").path("upload")
                              .request()
                              .header("Content-Type", MediaType.MULTIPART_FORM_DATA)  
                              .post(Entity.entity(multiPart, multiPart.getMediaType()));
    //.header("Content-Type", "multipart/form-data"
    //.header("Content-Type", "multipart/form-data;charset=UTF-8")
    //multiPart.getMediaType()
    //"Content-Tpye: multipart/form-data"
    System.out.println("--> " + response.getMediaType());
    System.out.println("--> " + response.getStringHeaders());

    System.out.println(":: RESPONSE : " + response);
    System.out.println(":: STATUSCODE: " + response.getStatus() + " -- Reason: " + response.getStatusInfo());
    String output = response.readEntity(String.class);      
    System.out.println(":: OUTPUT: " + output);

}

的WebService: 我尝试了两种不同的解决方案,但我找不到我的错误。

package vogella.jersey.first;

import java.io.File;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.server.ResourceConfig;

@Path("/hello")
public class Hello {


@GET
@Path("/test")
@Produces("application/xml")
public MyBean getBean() {
    return new MyBean("Hello MyBean", 42);
}

@POST
@Path("/test")
@Consumes("application/xml")
public String postMyBean(MyBean myBean) {
    return myBean.anyString;
}

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
//@Produces("multipart/mixed")
public MultiPart post (final FormDataMultiPart multiPart) {
    System.out.println("::: THIS IS THE MULTIPART MESSAGE: " + multiPart);
    return multiPart;
}

@POST
@Path("/ul")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("text/plain")
public Response uploadFile(final FormDataMultiPart form) {
    String output = "Done";
    return Response.status(200).entity(output).build();
}
}

Jersey REST files

我能够向服务器发送正常的GET和Post Request。但我也需要发送文件。我希望,有人可以帮助我。

这是我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<display-name>vogella.jersey.first</display-name>

<servlet>

<servlet-name>Jersey REST Service</servlet-name>
    <servlet-
class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
     <!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>vogella.jersey.first</param-value>
</init-param>
<!-- 
<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
-->
<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
   <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app> 

0 个答案:

没有答案