title: Java File Download - Downloaded file size is always zero Kb
body: <p>I have written a java controller which handles any download request from the server. My files are present in the server however I am getting 0 kB file downloaded every time. The file is getting downloaded but the size is always 0 Kb. Please help me. Here is my code - </p>
  
  <pre><code>@RequestMapping(value="/downloadFile/{docId}")
      public void getDownloadFile(@PathVariable(value = "docId") Integer docId, HttpServletResponse response) throws Exception {
          String userName = getUserName();
          try {
              DocVault documentsVault = documentsVaultRepository.findDocumentAttachment(docId);
              String fileName = documentsVault.getDocumentName();
              int customerId = documentsVault.getCustomerId();
  
              Map&lt;Integer, String&gt; customerInfo = cspUtils.getCustomersInfo(userName);
              Set&lt;Integer&gt; customerIds = customerInfo.keySet();
              for (int custId : customerIds) {
                  if (custId == customerId) {
                      String path = env.getProperty("doc.rootfolder") + File.separator + documentsVault.getFileName();
                      service.downloadFile(fileName, path, response);
                  } else {
                      logger.info("Customer not linked to user");
                  }
              }
          } catch (Exception e) {
              logger.error(e.getMessage(), e);
          }
      }
  </code></pre>
  
  <p>Implementation -</p>
  
  <pre><code>public void downloadFile(String fileName, String path, HttpServletResponse response) {
  
          try {
              File downloadFile = new File(path);
              FileInputStream inputStream = new FileInputStream(downloadFile);
  
              response.setContentLength((int) downloadFile.length());
              response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
  
              // get output stream of the response
              OutputStream outStream = response.getOutputStream();
  
              byte[] buffer = new byte[BUFFER_SIZE];
              int bytesRead = -1;
  
              // write bytes read from the input stream into the output stream
              while ((bytesRead = inputStream.read(buffer)) != -1) {
                  outStream.write(buffer, 0, bytesRead);
              }
              inputStream.close();
              outStream.close();
  
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>I am not getting any exceptions. Please help.
  Thank you in advance.</p>