JasperReports PdfServlet以PDF格式保存报告 - 如何设置浏览器的文件名以提示用户? [英] JasperReports PdfServlet to save report in PDF - how can I set the filename for the browser to prompt the user?

查看:157
本文介绍了JasperReports PdfServlet以PDF格式保存报告 - 如何设置浏览器的文件名以提示用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Stack JBF + PrimeFaces JBoss AS JasperReports

Stack: JSF + PrimeFaces on JBoss AS with JasperReports

我一直在使用JasperReports以PDF格式导出模式,只需三个步骤:

I have been using a pattern of exporting in PDF format using JasperReports with a three steps process:

[1]获取编译后的Jasper报告战争

[1] obtain the compiled Jasper report from a path in the war

[2]在会话中放置 JasperPrint 对象

[2] place the JasperPrint object on the session

[3]重定向到 PdfServlet的URL

因此,当用户从GUI点击 p:commandButton 调用支持bean的方法,该方法通过[1],[2]和[3],如下面的示例代码所示:

So when the user from the GUI clicks on a p:commandButton a backing-bean's method is called that goes through [1], [2] and [3] as in the following example code:

xhtml文件:

<p:commandButton ajax="false" action="#{indexController.exportPDF}" value="Export PDF"/>

支持bean代码:

private void putPrintObjectInSession() throws JRException {
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    ServletContext context = (ServletContext) externalContext.getContext();
    String reportFileName = context.getRealPath("/reports/PrimeNumbersReport.jasper");
    File reportFile = new File(reportFileName);
    if (!reportFile.exists())
        throw new JRRuntimeException(".jasper file not found in the war.");
    Map parameters = new HashMap();
    parameters.put("ReportTitle", "2nd Prime Numbers Report");
    parameters.put("BaseDir", reportFile.getParentFile());
    JasperPrint jasperPrint = 
            JasperFillManager.fillReport(
                      reportFileName, 
                      parameters, 
                      getSQLConnection()
                    );
    ((HttpSession) externalContext.getSession(false)).setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
}

public String exportPDF() throws IOException, JRException {
    putPrintObjectInSession();
    FacesContext facesContext = FacesContext.getCurrentInstance();  
    ExternalContext externalContext = facesContext.getExternalContext();  
    externalContext.redirect("servlets/pdf");
    return null;
}

我有两个问题:

[i]你看到这种方法有任何明显的代码味道或限制吗?

[i] do you see any obvious code smells or limitations with this approach?

[ii]使用Chrome和Conkeror上面的示例代码可以保存报告,但它们提供给用户保存文件的默认文件名只是pdf。如何将其配置为有意义的名称(例如report-2012-08-23c.pdf)?

[ii] with the example code above both Chrome and Conkeror can save the report but the default filename they present to the user for saving the file is simply "pdf". How can I configure that to a meaningful name (e.g. "report-2012-08-23c.pdf") ?

推荐答案

As对于具有另存为文件名的具体问题,它默认为请求URL中的最后一个路径(在 / servlets / pdf 的情况下,确实只是 pdf ),除非在 Content-Disposition 标题中另有说明。

As to your concrete problem with the "Save as" filename, it defaults to the last path in the request URL (which is in case of /servlets/pdf indeed just pdf), unless otherwise specified in Content-Disposition header.

问题不是由你的JSF代码直接造成的(尽管它本身就是奇怪的,但这是一个不同的问题/问题),但更多的是在servlet中它已映射在 / servlets / pdf 上。要设置所需的另存为文件名,您需要在将任何字节写入响应之前添加以下行:

The problem is not directly caused by your JSF code (although it is at its own kind of odd, but that's a different problem/question), but more in the servlet which is been mapped on /servlets/pdf. To set the desired "Save as" filename, you need to add the following line before writing any byte to the response:

response.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");

如有必要,您可以通过<替换附件 code>内联如果你想默认内联显示它。

You can if necessary replace attachment by inline if you want to display it by default inline.

但是,Internet Explorer浏览器会忽略此值并坚持使用使用请求URL中的最后一个路径。因此,为了覆盖该浏览器,您需要自己在请求URL中包含所需的文件名并更改servlet映射。

The Internet Explorer browser, however, ignores this value and sticks to using the last path in the request URL. So to cover that browser as well, you'd need to include the desired filename in the request URL yourself and change the servlet mapping.

例如

String filename = "report-2012-08-23c.pdf";
externalContext.redirect("servlets/pdf/" + filename);

with

@WebServlet("/servlets/pdf/*") // instead of @WebServlet("/servlets/pdf")

使用此URL模式,文件名位于可用的servlet中

With this URL pattern, the filename is inside the servlet available by

String filename = request.getPathInfo().substring(1);

这篇关于JasperReports PdfServlet以PDF格式保存报告 - 如何设置浏览器的文件名以提示用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆