Jersey client file upload code 415 - unsupported Media Type
<p>im trying to create a jersey client to upload files. I have a web serice and a client. But i get everytime a 415 - unsupported Media Type and i don't know what the problem could be.
I`ve searched the i-net / stackoverflow but i cant find a proper solution for my problem. I tried many solutions but without success. You can see this as comments in my code.</p>

<p>Client:</p>

<pre><code>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("--&gt; " + response.getMediaType());
    System.out.println("--&gt; " + 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);

}
</code></pre>

<p>WebService:
I tried 2 different solutions but i can't find my mistake.</p>

<pre><code>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();
}
}
</code></pre>

<p><a href="http://i.stack.imgur.com/zbXmo.jpg" rel="nofollow">Jersey REST files</a></p>

<p>Im able to send normal GET and Post Request to the server. But i need to send files too. I hope, someone can help me.</p>

<p>This is my web.xml:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;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"&gt;

&lt;display-name&gt;vogella.jersey.first&lt;/display-name&gt;

&lt;servlet&gt;

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