Problem with Apache POI exit

I have a problem with Apache POI. I am trying to return a file after processing the relevant data. When I go to return the file to the browser (both IE8 / 9, firefox), the browser returns a load of garbage characters. This only happens if the Excel file is large and the process runs for 2 minutes plus. Otherwise, it returns a file that can then be opened in Excel.

Any help is appreciated, thanks.

response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".xls\""); OutputStream out = null; try { out = new BufferedOutputStream(response.getOutputStream()); wb.write(out); out.flush(); } catch (IOException ex) { ex.printStackTrace(); } 
+6
source share
1 answer

I think you should specify the length of the content. This is the line you should insert:

 response.setContentLength(/* length of the byte[] */); 

I suggest you use the Apache Commons IOUtils class to work with byte arrays and streams.

+4
source

Source: https://habr.com/ru/post/887441/


All Articles