title: Local upload file via rest web service
body: <p>I am using the code in this link <a href="http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/" rel="nofollow">http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/</a>  to upload  a file.In this example I have to pass from a html page to specify the file to upload but I want to to acceed to it when I call the webservice by its path ( s.thing like that : http://<strong>*****</strong>:8080/RESTfulExample/file/upload/C://image.png)
  Are there any suggestions to this issue? Please help!
  That is what i did till now to solve it </p>
  
  <pre><code>@Path(value="/files")
  public class upload {
  @POST
  @Path(value = "upload/{path}")
  @Consumes("image/jpg")
  public Response uploadPng(@PathParam("path") String path, File file) throws IOException     {
      file = new File("path");
      String uploadedFileLocation = "C:/Users/Desktop/" + file.getName();
      DataInputStream diStream =new DataInputStream(new FileInputStream(file));
      long len = (int) file.length();
      byte[] fileBytes = new byte[(int) len];
      int read = 0;
      int numRead = 0;
      while (read &lt; fileBytes.length &amp;&amp; (numRead =
              diStream.read(fileBytes, read,fileBytes.length - read)) &gt;= 0) {
          read = read + numRead;
      }
      writeToFile(diStream, uploadedFileLocation);
      System.out.println("File uploaded to : " + uploadedFileLocation);
      return Response.status(200).entity(file).build();
    }
    private void writeToFile(InputStream uploadedInputStream,
                           String uploadedFileLocation) {
      try {
          OutputStream out =new FileOutputStream(new File(uploadedFileLocation));
          int read = 0;
          byte[] bytes = new byte[1024];
          out = new FileOutputStream(new File(uploadedFileLocation));
          while ((read = uploadedInputStream.read(bytes)) != -1) {
              out.write(bytes, 0, read);
          }
          out.flush();
          out.close();
      } catch (IOException e) {
          e.printStackTrace();
      }}}
  </code></pre>
  
  <p>But I have an 405 error now !! </p>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>@Path(value= "/up")
  public class upload {
  private static final String SERVER_UPLOAD_LOCATION_FOLDER =    "C://Users/Marwa/Desktop/mafile.png";
  @POST
  @Path(value="upload")
      @Consumes(MediaType.MULTIPART_FORM_DATA)
  public String uploadFile(@FormDataParam("file") InputStream fileInputStream) {
      String filePath = SERVER_UPLOAD_LOCATION_FOLDER ;
      System.out.println("*****serverpath********");
      saveFile(fileInputStream, filePath);
      String output = "File saved to server location : " + filePath;
      return output;
  }
   private void saveFile(InputStream uploadedInputStream,String serverLocation) {
      try {
      OutputStream outpuStream = new FileOutputStream(new File(serverLocation));
          int read = 0;
          byte[] bytes = new byte[1024];
          outpuStream = new FileOutputStream(new File(serverLocation));
          while ((read = uploadedInputStream.read(bytes)) != -1) {
          outpuStream.write(bytes, 0, read);}
          outpuStream.flush();
          outpuStream.close();
      } catch (IOException e) { 
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>}</p>