使用JSF在新的浏览器窗口中读取和打开PDF

使用JSF在新的浏览器窗口中读取和打开PDF

本文介绍了使用JSF在新的浏览器窗口中读取和打开PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java / JSF

Java / JSF

我试图在 一个新的浏览器窗口 中打开PDF而不是下载它在每次尝试时,文件都会成功下载并打开一个仅包含应用程序而不是pdf文件的新选项卡。

Im trying to open PDF in a new browser window instead of download it but in every try the file is downloaded with success and it opens a new tab with the application only and not the pdf file.

<p:commandLink title="report" target="_blank"
    action="#{managedBean.generateReport('P',true)}"
    ajax="false" immediate="false" >
</p:commandLink>

托管bean :generateReport调用downloadFile

Managed bean : generateReport calls downloadFile

下面的filePath参数= /temp/doc/abc.pdf(chmod 777)

The filePath parameter below = /temp/doc/abc.pdf (chmod 777)

public static void downloadFile(String filePath) throws IOException{
    FacesContext context = FacesContext.getCurrentInstance();
    HttpServletResponse response = (HttpServletResponse) context
                         .getExternalContext().getResponse();
    File file = new File(filePath);
    if (!file.exists()) {
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
     }
    response.reset();
    response.setBufferSize(DEFAULT_BUFFER_SIZE);
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Length", String.valueOf(file.length()));
    response.setHeader("Content-Disposition", "attachment;filename=\""
           + file.getName() + "\"");
    BufferedInputStream input = null;
    BufferedOutputStream output = null;

    try
    {
        input = new BufferedInputStream(new FileInputStream(file),
                    DEFAULT_BUFFER_SIZE);
        output = new BufferedOutputStream(response.getOutputStream(),
                        DEFAULT_BUFFER_SIZE);
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        int length;
        while ((length = input.read(buffer)) > 0) {
            output.write(buffer, 0, length);
        }
    } finally
    {
        input.close();
        output.close();
    }
    context.responseComplete();
}

我的Chromium插件已启用:

My Chromium plugin is enabled :

推荐答案

primefaces media

How about primefaces media :

来自:

<p:media value="/resources/other/guide.pdf" width="100%" height="300px"/>

此外,您可以将此PDF查看器放在 p:对话框中

Also you can put this PDF viewer in a p:dialog here.

这篇关于使用JSF在新的浏览器窗口中读取和打开PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 13:31