【发布时间】:2017-06-08 00:21:46
【问题描述】:
您好,我一直在尝试编写代码,以便我可以生成一个 excel 表,并在用户单击下载按钮时下载它....我已经成功生成了 excel 表,但我尝试下载相同的表但我没有成功。
我使用的方法是:
public void download() throws IOException {
File file = new File("D:\\pdf\\carrierReport7.xls");
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
response.setHeader("Content-Type", "application/vnd.ms-excel");
OutputStream outputStream = response.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(file);
byte[] bytesBuffer = new byte[2048];
int bytesRead = 0;
while ((bytesRead = fileInputStream.read(bytesBuffer)) > 0) {
outputStream.write(bytesBuffer, 0, bytesRead);
}
outputStream.flush();
fileInputStream.close();
outputStream.close();
facesContext.responseComplete();
}
jsf 命令:
【问题讨论】:
-
您是否看到错误/异常?当您尝试此操作时,是否有任何内容实际下载到您的浏览器?
-
尝试在 stackoverflow 中找到其中一个重复项,并检查其中哪些部分对您有帮助。 Nicolas Smith 是对的……您可以而且应该提供更多信息。
-
没有错误。它只是处理请求而没有响应。也没有下载。我尝试了stackoverflow中已经给出的不同代码,但我仍然面临同样的问题。我是否需要对 web.xml 或浏览器设置进行任何更改