如何使pdf显示为下载选项而不是在浏览器上呈现?

时间:2013-08-21 05:58:49

标签: java http rest jasper-reports download

我在项目中使用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");

负责下载选项。虽然我把它包含在代码中,但它在响应中没有任何地方。请指教。

1 个答案:

答案 0 :(得分:2)

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

我认为您需要在写入响应输出流之前先设置响应头。