【发布时间】:2015-06-16 12:38:13
【问题描述】:
在我的应用程序中,我使用 jsf 和 richfaces,我想在 Web 浏览器中显示生成的 pdf,我已经在服务器端生成了这个 PDF,它在 ServletResponse 中可用,但我无法在我的网页中显示它。
我尝试了this 的问题,但它并没有解决我的问题。
是否有任何库或特殊方法可以做到这一点?
下面给出了我尝试做的事情。
<h:commandLink rendered="#{ fileImportOptionsViewBean.isFileExist(status.myPdf) }" action="#
{fileImportOptionsViewBean.downloadFile(satus.myPdf)}" target="_blank">
<h:graphicImage url="../../resources/xyz/icons/pdf.png" /></h:commandLink>
下载文件方法
public void downloadFile(String filePath){
try {
File file=new File(filePath);
if(file.exists()){
FacesContext fc=FacesContext.getCurrentInstance();
ExternalContext ec=fc.getExternalContext();
ec.responseReset();
ec.setResponseContentType("application/pdf");
ec.setResponseHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
ec.setResponseBufferSize(8192);
OutputStream output = ec.getResponseOutputStream();
URL url = file.toURL();
try(
BufferedInputStream bis = new BufferedInputStream(url.openStream());
BufferedOutputStream bos = new BufferedOutputStream(output);
){
byte[] buff = new byte[8192];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
}
fc.responseComplete();
}
} catch (Exception e) {
}
}
感谢任何帮助,谢谢。
【问题讨论】:
-
不,在downloadFile方法执行完美,我相信我用jsf做的也可以,但是新打开的窗口中没有呈现响应。
-
您发布的代码的当前行为是什么?究竟会发生什么?