title: REST servie with client to upload file
body: <p>I building a REST service that would upload a WAR file on some machine(remote or local) with the following code:</p>
  
  <pre><code>import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  import javax.ws.rs.*;
  import javax.ws.rs.core.MediaType;
  import javax.ws.rs.core.Response;
  
  import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
  import org.glassfish.jersey.media.multipart.FormDataParam;
  
  @Path("/upload")
  public class FileUpload {
      @POST
      @Path("/pdf")
      @Consumes({MediaType.MULTIPART_FORM_DATA})
      public Response uploadPdfFile(  @FormDataParam("file") InputStream fileInputStream,
                                      @FormDataParam("file") FormDataContentDisposition fileMetaData) throws Exception
      {
          String UPLOAD_PATH = "D:/";
          try
          {
              int read = 0;
              byte[] bytes = new byte[1024];
  
              OutputStream out = new FileOutputStream(new File(UPLOAD_PATH + fileMetaData.getFileName()));
              while ((read = fileInputStream.read(bytes)) != -1) 
              {
                  out.write(bytes, 0, read);
              }
              out.flush();
              out.close();
          } catch (IOException e) 
          {
              throw new WebApplicationException("Error while uploading file. Please try again !!");
          }
          return Response.ok("Data uploaded successfully !!").build();
      }
  
  }
  </code></pre>
  
  <p>And the client java code is as follows:</p>
  
  <pre><code>import java.io.File;
  import java.io.IOException;
  
  import javax.ws.rs.client.Client;
  import javax.ws.rs.client.ClientBuilder;
  import javax.ws.rs.client.Entity;
  import javax.ws.rs.client.WebTarget;
  import javax.ws.rs.core.Response;
  
  import org.glassfish.jersey.media.multipart.FormDataMultiPart;
  import org.glassfish.jersey.media.multipart.MultiPartFeature;
  import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
  
  public class FileUploadClient {
  
      public static void main(String[] args) throws IOException 
      {
          final Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();
  
          final FileDataBodyPart filePart = new FileDataBodyPart("file", new File("C:/Users/Jainesh_Trivedi/Desktop/WAR/AutohostDemo1_1145.war"));
          FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
          final FormDataMultiPart multipart = (FormDataMultiPart) formDataMultiPart.field("foo", "bar").bodyPart(filePart);
  
          final WebTarget target = client.target("http://localhost:10001/JerseyFileUpload/rest/upload/pdf");
          final Response response = target.request().post(Entity.entity(multipart, multipart.getMediaType()));
  
          //Use response object to verify upload success
  
          formDataMultiPart.close();
          multipart.close();
      }
  
  
  }
  </code></pre>
  
  <p>Kindly help me with this as I'm getting the following error:</p>
  
  <pre><code>    Exception in thread "main" javax.ws.rs.ProcessingException: java.io.IOException: Error writing to server
      at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:229)
      at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:246)
      at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:667)
      at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:664)
      at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
      at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
      at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
      at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
      at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:664)
      at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:424)
      at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:333)
      at com.fileupload.rest.FileUploadClient.main(FileUploadClient.java:27)
  Caused by: java.io.IOException: Error writing to server
      at sun.net.www.protocol.http.HttpURLConnection.writeRequests(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.writeRequests(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
      at java.net.HttpURLConnection.getResponseCode(Unknown Source)
      at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:321)
      at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:227)
      ... 11 more
  </code></pre>