如何在 JSF 中显示 PDF,内容来自 ServletResponse

How to display PDF in JSF, with content from ServletResponse

在我的应用程序中,我使用 jsf 和 richfaces,我想在网络浏览器中显示生成的 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) {

    }

}

感谢任何帮助,谢谢。

我没有发现您当前的 setup.Most 有任何问题,可能问题出在您的 XHTML 页面上,某些东西导致您的 h:commandLink 无法触发 event.Please 引用 this post 了解更多详情,一定会对您有所帮助。