title: Java Jersey - How can i write a file in my Tomcat Server
body: <p>I got a problem in writing the file to the localhost of the tomcat server in my system.</p>
  
  <p>How can i write file in the location mentioned Below ?? I am getting a response as the file uploaded in the location mentioned. But i don't know where it is stored in my system.. Is it the right way of doing ?? </p>
  
  <pre><code>@POST
  @Path("/upload")
  @Consumes(MediaType.MULTIPART_FORM_DATA)
  public Response uploadFile(
          @FormDataParam("file") InputStream uploadedInputStream,
          @FormDataParam("file") FormDataContentDisposition fileDetail) {
  
      String uploadedFileLocation = "http://127.0.0.1:8080/Attachments"
              + fileDetail.getFileName();
  
      // save it
      writeToFile(uploadedInputStream, uploadedFileLocation);
  
      String output = "File uploaded to : " + uploadedFileLocation;
  
      return Response.status(200).entity(output).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>Actually I got this [writeToFile] code from <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></p>