【问题标题】:How to make a pdf appear as a download option rather than rendering on the browser?如何使 pdf 显示为下载选项而不是在浏览器上呈现?
【发布时间】:2013-08-21 05:58:48
【问题描述】:

我在我的项目中使用 Jasper Reports 来生成多种格式的报告。尽管所有代码示例都运行良好,但我遇到了一个概念问题。

我编写了这个在浏览器上生成 PDF 作为下载选项的简单代码:

    @GET
    @Path("/basicdbreport")
    @Produces("application/pdf")
    public Response basicDbReport(@Context ServletContext context,
            @Context HttpServletResponse response) throws JRException,
            IOException {
        Connection connection = null;
        byte[] reportBytes = null;
        String reportName = "firstsqlexample";

        File file = new File(path + reportName + JASPER_EXTENSION);

        // check if compiled report exists
        if (!file.exists()) {
            compileReport(context.getRealPath(path + reportName + JRXML_EXTENSTION));
        }

        // input stream for filling the compiled report
        InputStream compiledReportStream = context.getResourceAsStream(path
                + reportName + JASPER_EXTENSION);

        try {
            connection = dataSource.getConnection();
            reportBytes = JasperRunManager.runReportToPdf(compiledReportStream,
                    new HashMap<String, Object>(), connection);

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (reportBytes != null) {
            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(reportBytes);
        }

        ResponseBuilder restResponse = Response.ok();
        restResponse.header("Content-Disposition",
                "attachment; filename=firstSQLReport.pdf");
        return restResponse.build();
    }

代码运行良好,我在浏览器中收到下载提示。然而,当我深入研究 Jasper API 时,我发现了一个方法 runReportToPdfStream() 方法,它可以为我处理输出流。

新代码如下所示:

    @GET
    @Path("/basicdbreport")
    @Produces("application/pdf")
    public Response basicDbReport(@Context ServletContext context,
            @Context HttpServletResponse response) throws JRException,
            IOException {
        ServletOutputStream outputStream = response.getOutputStream();
        Connection connection = null;
        String reportName = "firstsqlexample";

        File file = new File(path + reportName + JASPER_EXTENSION);

        // check if compiled report exists
        if (!file.exists()) {
            compileReport(context.getRealPath(path + reportName + JRXML_EXTENSTION));
        }

        // input steam to fill complied report
        InputStream compiledReportStream = context.getResourceAsStream(path
                + reportName + JASPER_EXTENSION);

        try {
            connection = dataSource.getConnection();
            JasperRunManager.runReportToPdfStream(compiledReportStream, outputStream, new HashMap<String, Object>(), connection);

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        ResponseBuilder restResponse = Response.ok();
        restResponse.header("Content-Disposition",
                "attachment; filename=firstSQLReport.pdf");
        return restResponse.build();
    }

代码运行良好,但 我没有收到任何下载提示,而是在浏览器上呈现 pdf。响应头如下(在浏览器上):

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
Date: Wed, 21 Aug 2013 05:51:42 GMT

代码现在无法提供下载提示的原因是什么?我不是 HTTP 高手,但我猜是这样的:

restResponse.header("Content-Disposition",
                    "attachment; filename=firstSQLReport.pdf");

负责下载选项。尽管我确实将它包含在代码中,但它在响应中没有任何位置。请指教。

【问题讨论】:

    标签: java http rest jasper-reports download


    【解决方案1】:

    是的,您是对的,Content-Disposition 是您需要设置的响应标头,以触发客户端浏览器上的下载操作。

    我认为您需要先设置响应标头,然后再写入响应输出流。

    【讨论】:

      猜你喜欢
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-07
      • 2016-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多