title: JAVA, Jersy POST-ing files to server and GET-ing files from server. Using multiPart
body: <p>Code: </p>
  
  <pre><code>import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.InputStream;
  
  import javax.ws.rs.client.Entity;
  import javax.ws.rs.core.MediaType;
  
  import org.glassfish.jersey.client.ClientResponse;
  import org.glassfish.jersey.media.multipart.MultiPart;
  import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
  
  import com.itz.passport.authentication.Int3DPassport;
  import com.sun.jersey.api.client.*;
  import com.sun.jersey.api.client.config.ClientConfig;
  import com.sun.jersey.api.client.config.DefaultClientConfig;
  import com.sun.jersey.multipart.impl.MultiPartWriter;
  
  public class UploadFileRestClient {
      private WebResource webResource;
      private Client client;
      private String url = "http://development/server/resources/FileService";
  
      public UploadFileRestClient() {
          try {
              this.client = Client.create(new DefaultClientConfig());
              this.webResource = client.resource(url).path("/file");
  
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public ClientResponse upload() {
          try {
              Int3DPassport passportAut = new InPassport();
              String host = "http://development/server";
              String userName = "admin";
              String password = "xxxxx";
              String ticket = IntPassport.getTicket(host, userName, password);
              System.out.println("ticket:::::" + ticket);
  
              WebResource resource = this.webResource;
              MultiPart multiPart = new MultiPart();
              multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
              FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("photo", new File("D://Documents//Test.txt"));
              multiPart.bodyPart(fileDataBodyPart);
  
              // here Iam getting error, return statement.
              return resource.path("upload"+ticket).type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, multiPart);
  
          } catch (Exception e) {
              e.printStackTrace();
          }
          return null;
      }
  
  }
  
  </code></pre>
  
  <p>Iam getting exception at in upload() at return statement. so it going to catch and giving null pointer exception. I have googled for this error com.sun.jersey.api.client.ClientHandlerException and nothing resolved my error. I have added mimepull.jar and jeresey-multipart.jar. Still issue is not solved.</p>
  
  <pre><code>com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class org.glassfish.jersey.media.multipart.MultiPart, and MIME media type, multipart/form-data, was not found
      at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:155)
      at com.sun.jersey.api.client.Client.handle(Client.java:652)
      at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)
      at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
      at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:570)
      at Test.main.UploadFileRestClient.upload(UploadFileRestClient.java:66)
      at Test.main.Main.main(Main.java:38)
  Caused by: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class org.glassfish.jersey.media.multipart.MultiPart, and MIME media type, multipart/form-data, was not found
      at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:288)
      at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:217)
      at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:153)
      ... 6 more
  Exception in thread "main" java.lang.NullPointerException
      at Test.main.Main.main(Main.java:40)
  </code></pre>
  
  <p>Thanks for help and suggestions.</p>