title: Returning excel file using spring boot controller
body: <p>I was trying to make a rest endpoint in Spring Boot which reads from DB, generates an excel file(Using Apache POI) which is returned to the user using HttpServletResponse but when I invoke this, the excel is getting created but it's not downloading. I had some other code earlier in place which was working fine but I accidentally removed that and now I'm stuck. Any help/leads are appreciated.</p>
  
  <pre><code>@RequestMapping(path = "/save", method = RequestMethod.GET)
  public ResponseEntity&lt;String&gt; saveToXls(@RequestParam String id, @RequestParam String appName, HttpServletResponse response) {
      AppInstance appInstance = appInstanceRepo.get(id);
      List&lt;DownloadDetail&gt; downloadDetailList = downloadDAO.searchByInstanceId(id);
      //List&lt;DownloadDetail&gt; downloadDetailList = appInstance.getDownloads();
      System.out.print("LIST SIZE:" + downloadDetailList.size());
      String fileName = appName + " report";
      File myFile = new File(fileName + ".xls");
  
      FileOutputStream fileOut;
      downloadDetailList.forEach(downloadDetail -&gt; System.out.print(downloadDetail.getSid()));
      try {
  
          try (HSSFWorkbook workbook = new HSSFWorkbook()) {
              HSSFSheet sheet = workbook.createSheet("lawix10");
              HSSFRow rowhead = sheet.createRow((short) 0);
              rowhead.createCell((short) 0).setCellValue("SID");
              rowhead.createCell((short) 1).setCellValue("Download Time");
              rowhead.createCell((short) 2).setCellValue("OS Version");
              int i = 0;
  
              for (DownloadDetail downloadDetail : downloadDetailList) {
                  System.out.print("In loop -2");
                  HSSFRow row = sheet.createRow((short) i);
                  row.createCell((short) 0).setCellValue(downloadDetail.getSid());
                  row.createCell((short) 1).setCellValue(downloadDetail.getDownloadTime());
                  row.createCell((short) 2).setCellValue(downloadDetail.getOsVersion());
                  i++;
              }
              fileOut = new FileOutputStream(myFile);
              workbook.write(fileOut);
          }
          fileOut.close();
  
          byte[] buffer = new byte[10240];
          response.addHeader("Content-disposition", "attachment; filename=test.xls");
          response.setContentType("application/vnd.ms-excel");
          try (
                  InputStream input = new FileInputStream(myFile);
                  OutputStream output = response.getOutputStream();
          ) {
              for (int length = 0; (length = input.read(buffer)) &gt; 0;) {
                  output.write(buffer, 0, length);
              }
          }
  
          response.flushBuffer();
  
      } catch (FileNotFoundException e1) {
          e1.printStackTrace();
      } catch (IOException e1) {
          e1.printStackTrace();
      }
  
  
      return null;
  }
  </code></pre>
  
  <p>EDIT:
  I tried to do it another way as shown below:</p>
  
  <pre><code>        try (InputStream is = new FileInputStream(myFile)) {
              response.addHeader("Content-disposition", "attachment; filename=test.xls");
              response.setContentType("application/vnd.ms-excel");
              IOUtils.copy(is, response.getOutputStream());
  
          }
      response.flushBuffer();
  </code></pre>
  
  <p>This also doesn't seem to cut it.</p>