我想我已把所有部分放在一起提供下载文件。相反,下面的代码是在浏览器本身上写字节数组。我确信byte []没有任何问题,因为我之前使用FileOutputStream来生成excel文件。
FacesContext fc = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse)fc.getExternalContext().getResponse();
InputStream templateInputStream=null;
try {
templateInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(SOME_PATH);
if(templateInputStream==null) {
throw new WebException("Template not found at: "+SOME_PATH);
}
byte[] suppData= ExcelRenderer.render(templateInputStream, data);
response.reset();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename=\""+REPORT_FILE_NAME+"\"");
response.getOutputStream().write(suppData);
response.flushBuffer();
fc.responseComplete();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if (templateInputStream!=null){
templateInputStream.close();
}
}