Java HttpServlet how to download excel file -


I am trying to add a function to my web app that lets users download the excel file.

I am trying to get it with the following code:

  @Override Public Zero doPost (HTTPProvinceRequest request request, HTTPPreserve response) (file file = new file ("D: /test/test.xls"); response.setContentType ("application / xls"); Response.addHeader ("content-dispute", "attachment; file name = test.xls"); Response.setContentLength (Int) file.length ()); {FileInputStream fileInputStream = Try New FileInputStream (File); OutputStream ResponseOperatusStream = Response.GetOutputStart Reim (); int bytes; while ((byte = fileInputStream.read ())! = -1) {responseOutputStream.write (bytes);} FileInputStream.close (); responseOutputStream.close ();} hold (FileNotFoundException e) {// TODO Automatic Holding Block e.printStackTrace ();} Hold (IOException E) {// TODO Auto-Generated Catch Block e.printStackTrace ();}}  

I I am able to download the excel file with the above code, although the file is corrupted if I open it with Microsoft Excel, then I get a popup with the message:

"File format And do not get expanded. The file may be corrupted or unsafe ".

And the excel file is empty

After running the code, the original file (D: / test / test .xl) also gets corrupted .

Official MIME type for Excel file .xls < / Code> for application / vnd.ms-excel and .xlsx application / vnd.openxmlformats-officedocument.spreadsheetml.sheet . < / P>

In addition, I suggest that response.reset () before writing in the output stream and responseOutputStream.flush () (important) response .

before the closing

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -