支持 bean 中生成的二进制文件下载在客户端中显示为纯文本
Binary file download generated in backing bean appears as plain text in client
我有服务器端 web 应用程序,我正在尝试使用 Apache POI 生成一个 excel 文件,该文件由支持 bean 下载提供。我可以将文件写入服务器磁盘,但不能将其写入 HTTP 响应。
这是我的代码
public void createExcel(){
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
HSSFWorkbook workBook = new HSSFWorkbook();
HSSFSheet sheet = workBook.createSheet("hello world");
HSSFRow row = sheet.createRow((short) 0);
HSSFCell cell;
cell = row.createCell(0);
cell.setCellValue(new HSSFRichTextString("Hello"));
cell = row.createCell(1);
cell.setCellValue(new HSSFRichTextString("world"));
workBook.write(bos);
bos.flush();
FacesContext fc = FacesContext.getCurrentInstance();
downloadExcel(bos, fc);
} catch (FileNotFoundException e) {
log.debug("file not found ", e);
} catch (IOException e){
log.debug("IOException ", e);
}
}
public void downloadExcel(ByteArrayOutputStream baos, FacesContext fc){
HttpServletResponse response = (HttpServletResponse)fc.getExternalContext().getResponse();
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", "attachment; filename=testxls.xls");
try {
ServletOutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();
out.close();
} catch (IOException e) {
log.error("IOException ", e);
}
fc.responseComplete();
}
结果如下
我试过的所有浏览器都会出现这种情况:Chrome、FF 和 IE。希望您能指出我在这里遗漏的内容。
当您导航您的 servlet 时,浏览器不会打开一个对话框来邀请您下载文件?我想原因是您错过了将文件名括在引号之间:
response.addHeader("Content-Disposition", "attachment; filename=\"testxls.xls\"");
终于在给定 link 的 BalusC 中找到了解决方案。我在 <a4j:commandButton>
标记内调用了 JSF 中的 createExcel()
方法,它通过 ajax 调用了方法。将其更改为 <h:commandButton>
,现在可以使用了。
我有服务器端 web 应用程序,我正在尝试使用 Apache POI 生成一个 excel 文件,该文件由支持 bean 下载提供。我可以将文件写入服务器磁盘,但不能将其写入 HTTP 响应。
这是我的代码
public void createExcel(){
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
HSSFWorkbook workBook = new HSSFWorkbook();
HSSFSheet sheet = workBook.createSheet("hello world");
HSSFRow row = sheet.createRow((short) 0);
HSSFCell cell;
cell = row.createCell(0);
cell.setCellValue(new HSSFRichTextString("Hello"));
cell = row.createCell(1);
cell.setCellValue(new HSSFRichTextString("world"));
workBook.write(bos);
bos.flush();
FacesContext fc = FacesContext.getCurrentInstance();
downloadExcel(bos, fc);
} catch (FileNotFoundException e) {
log.debug("file not found ", e);
} catch (IOException e){
log.debug("IOException ", e);
}
}
public void downloadExcel(ByteArrayOutputStream baos, FacesContext fc){
HttpServletResponse response = (HttpServletResponse)fc.getExternalContext().getResponse();
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", "attachment; filename=testxls.xls");
try {
ServletOutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();
out.close();
} catch (IOException e) {
log.error("IOException ", e);
}
fc.responseComplete();
}
结果如下
我试过的所有浏览器都会出现这种情况:Chrome、FF 和 IE。希望您能指出我在这里遗漏的内容。
当您导航您的 servlet 时,浏览器不会打开一个对话框来邀请您下载文件?我想原因是您错过了将文件名括在引号之间:
response.addHeader("Content-Disposition", "attachment; filename=\"testxls.xls\"");
终于在给定 link 的 BalusC 中找到了解决方案。我在 <a4j:commandButton>
标记内调用了 JSF 中的 createExcel()
方法,它通过 ajax 调用了方法。将其更改为 <h:commandButton>
,现在可以使用了。