Kovacs Kovacs -4 years ago 136
Java Question

java HttpServletResponse fileSize over 8k

When the file size is less than 8k, the file name display correctly,
When more than 8k, can not display, after modify the file suffix, open the file, the data is output completely.How to do

response.reset();
OutputStream fileoutStream = response.getOutputStream();
hssfWorkbook.write(fileoutStream);
String filename = new String(wbname.getBytes(), "ISO-8859-1");
resp.setContentType("application/vnd.ms-excel,charset=utf-8");
resp.addHeader("Content-Disposition", "attachment;filename=\""+filename+".xls\"");


enter image description here

Answer Source

You need to add the headers before writing to the OutputStream. It works when the file is small enough because the output is buffered, but technically I think that's a bug. The HTTP protocol requires that all the headers are sent to the client first, and then the payload. You can't go back and add headers once the first byte of the payload is flushed to the socket.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download