簡體   English   中英

如何用ServletResponse中的內容在JSF中顯示PDF

[英]How to display PDF in JSF, with content from ServletResponse

在我的應用程序中,我使用jsf和richfaces,我想在Web瀏覽器中顯示生成的pdf,我已經在服務器端生成了該PDF,並且它在ServletResponse中可用,但是無法在我的網頁中顯示。

我已經試過這個問題,但是並不能解決我的問題。

是否有任何庫或特殊方法可以做到這一點?

下面是我嘗試做的事情。

    <h:commandLink rendered="#{ fileImportOptionsViewBean.isFileExist(status.myPdf) }" action="#
    {fileImportOptionsViewBean.downloadFile(satus.myPdf)}" target="_blank">


<h:graphicImage url="../../resources/xyz/icons/pdf.png" /></h:commandLink>

downloadFile方法

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) {

    }

}

感謝您的任何幫助,謝謝。

我目前的設置沒有任何問題。問題很可能出在您的XHTML頁面上,並且是導致h:commandLink無法觸發該事件的原因。請參閱帖子以獲取更多詳細信息,當然這會對您有所幫助給你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM