title: Dropwizard file upload
body: <p>I have to upload a file from my site yet cnt seem to get it working with drop wizard.</p>
  
  <p>Here is the form from my site.</p>
  
  <pre><code>   &lt;form enctype="multipart/form-data" method="POST" action="UploadFile"&gt;
       &lt;input type="file" id="fileUpload" name="file"/&gt;
       &lt;input type="hidden" id="fileName" name="fileName"/&gt;
       &lt;input type="submit" value="Upload"/&gt;
     &lt;/form&gt;
  </code></pre>
  
  <p>How would I go about on the backend to receive the file?</p>
  
  <p>The solution was</p>
  
  <pre><code>    @POST
      @Consumes(MediaType.MULTIPART_FORM_DATA)
      public Response uploadFile(
          @FormDataParam("file") final InputStream fileInputStream,
          @FormDataParam("file") final FormDataContentDisposition contentDispositionHeader) {
  
  
      String filePath = uploadLocation + newFileName;
      saveFile(fileInputStream, filePath);
      String output = "File can be downloaded from the following location : " + filePath;
  
      return Response.status(200).entity(output).build();
  
  }
  
  private void saveFile(InputStream uploadedInputStream,
          String serverLocation) {
      try {
              OutputStream outputStream = new FileOutputStream(new      File(serverLocation));
              int read = 0;
              byte[] bytes = new byte[1024];
  
              outputStream = new FileOutputStream(new File(serverLocation));
              while ((read = uploadedInputStream.read(bytes)) != -1) {
                  outputStream.write(bytes, 0, read);
              }
              outputStream.flush();
              outputStream.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }   
  </code></pre>