【问题标题】:How to display PDF in JSF, with content from ServletResponse如何在 JSF 中显示 PDF,内容来自 ServletResponse
【发布时间】: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做的也可以,但是新打开的窗口中没有呈现响应。
  • 您发布的代码的当前行为是什么?究竟会发生什么?

标签: jsf pdf jsf-2 richfaces


【解决方案1】:

我没有发现您当前的设置有任何问题。很可能问题出在您的 XHTML 页面上,并且某些原因导致您的 h:commandLink 没有触发该事件。请参阅this 帖子以获取更多详细信息,当然是这个会对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 2019-09-24
    • 2021-03-27
    相关资源
    最近更新 更多