0
/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    System.out.println("Hi inside download file :");
    response.setContentType("application/vnd.ms-excel");
    PrintWriter out = response.getWriter();
    String filename = "ResetPassword.xlsx";
    InputStream is = null;
    //is = this.getClass().getClassLoader().getResourceAsStream("LoginConfigurations.xlsx");
    //System.out.println("InputStream :"+is);
    String filepath = "E:\\SPACE_OM_01.02\\SPACE-OM_1.0-V01.02\\WebContent\\Data\\";
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");

    // use inline if you want to view the content in browser, helpful for
    // pdf file
    response.setHeader("Content-Disposition","inline; filename=\"" +filename + "\"");
    FileInputStream fileInputStream = new FileInputStream(filepath+ filename);
    /*BufferedReader br = new BufferedReader(new InputStreamReader(is));

        int line;
        System.out.println("Buffer Reader length :"+br.read());
        StringBuilder sb = new StringBuilder();
        while ((line = br.read()) != -1) {
            //sb.append(line);
            out.write(line);
        }
        br.close();*/
    int line;
    System.out.println("Buffer Reader length :"+fileInputStream.read());
    //StringBuilder sb = new StringBuilder();
    while ((line = fileInputStream.read()) != -1) {
        //sb.append(line);
        out.write(line);
    }
    fileInputStream.close();
    out.close();
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
}

I am trying to download the excel file with extension .xlsx using button click after clicking download button file is downloading when i am trying to open that download file its giving error like Excel file cannot open because the file format or file extensions is not valid. Please help me in solving this problem. Thanks in advance.

Shilpa
  • 19
  • 4
  • Think the Content is wrong try this solution: http://stackoverflow.com/questions/2937465/what-is-correct-content-type-for-excel-files – Jens Jul 29 '16 at 05:14
  • `response.setContentType("application/vnd.ms-excel")`, then `response.setContentType("application/octet-stream")`. Make up your mind. I suggest your mind stick with the first one. – Andreas Jul 29 '16 at 05:33
  • `response.setHeader("Content-Disposition", "attachment...`, then `response.setHeader("Content-Disposition","inline...`. Make up your mind. – Andreas Jul 29 '16 at 05:34

1 Answers1

1

Replace this line:

PrintWriter out = response.getWriter();

with this line:

OutputStream out = response.getOutputStream();

You're reading a byte from an InputStream and you should be writing a byte to an OutputStream again to copy it. Right now your byte is sent as a character to a PrintWriter which is wrong.

And remove this line:

System.out.println("Buffer Reader length :"+fileInputStream.read());

This line is reading the first byte from the file and throwing it away, and as a result the downloaded file is missing its first byte.

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • OP should also decide on a Content-Type and a Content-Disposition, instead of setting both twice. --- Using try-with-resources would also be a good suggestion. – Andreas Jul 29 '16 at 05:35
  • @Andreas Agree. Also the OP should do a lot more to clean up this code. It's very inefficient as well. At least it looks like he got *some* file downloaded, just that the file was corrupt. – Erwin Bolwidt Jul 29 '16 at 05:38