title: Unable to download excel file using Java
body: <p>I have a requirement to download excel to the excel file on client side using servlet. I am first creating an excel by fetching the values from mysql table and then downloading it. The excel is getting created in the path provided but I am unable to download it. Please find my code below for downloading:</p>
  
  <pre><code>String exportFileName = ResourceConstants.EXCEL_FILEPATH+"/"+ResourceConstants.EXCEL_FILENAME;
  
          FileInputStream inf = null;
  
          response.setContentType("application/vnd.ms-excel");
          response.addHeader("content-disposition",
                  "attachment; filename=" + exportFileName);
          response.setHeader("Pragma", "public");
          response.setHeader("Cache-Control", "no-store");
          response.addHeader("Cache-Control", "max-age=0");
          final int size = 1024;
          try{
              inf = new FileInputStream(exportFileName);
              response.setContentLength(inf.available());
              final byte[] buffer = new byte[size];
              ServletOutputStream os = null;
              os = response.getOutputStream();
              int length = 0;
              while ((length = inf.read(buffer)) != -1) {
                  os.write(buffer, 0, length);
              }
              inf.close();
              os.flush();
              os.close();
          } catch (final FileNotFoundException e) {
              LOGGER.error("FileNotFoundException occurred in download excel method!!!"+e.getMessage());
          }
  </code></pre>
  
  <p>Please help me understand if I am doing something wrong here.</p>