Apache POI输出问题

Cli*_*ive 6 java excel tomcat http apache-poi

我有Apache POI的问题.在处理完相关数据后,我尝试返回一个文件.当我将文件返回浏览器(IE8/9,firefox)时,浏览器返回一堆垃圾字符.这仅在Excel文件很大且进程已经运行了2分钟时才会发生.否则它返回一个文件然后我可以在Excel中打开.

任何帮助表示赞赏,谢谢.

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();
}
Run Code Online (Sandbox Code Playgroud)

jab*_*bal 4

我认为您还应该指定内容长度。这是您应该插入的行:

response.setContentLength(/* length of the byte[] */);
Run Code Online (Sandbox Code Playgroud)

我建议您使用 Apache Commons IOUtils 类来处理字节数组和流。