开发者

Apache POI output problem

I have a problem with Apache POI. I attempt to return a file after I have processed 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 when the Excel file is large and the process has been running for say 2 minutes plus. Otherwise it returns a file which I can then open in Excel.

Any help is appreciated, thanks.

response.setContentType("application/vnd.ms-excel");
response.setHeader("C开发者_如何转开发ontent-Disposition", "attachment; filename=\"" + fileName + ".xls\"");
OutputStream out = null;

try {
  out = new BufferedOutputStream(response.getOutputStream());
  wb.write(out);
  out.flush();     
} catch (IOException ex) {
  ex.printStackTrace();
}


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

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

I suggest you using Apache Commons IOUtils class for dealing with byte arrays and streams.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜