繁体   English   中英

如何从servlet传输pdf文档?

[英]How to stream pdf document from servlet?

我正在使用jasper报告创建pdf文档,我需要从servlet中传输该pdf文档。任何人都可以帮助我在哪里出错。这是我在我的应用程序中使用的代码片段。

ServletOutputStream servletOutputStream = response.getOutputStream();

String fileName="test.pdf";

response.setContentType("application/pdf");
response.setHeader("Content-Disposition","attachment; filename=\"" + fileName + "\"");
response.setHeader("Cache-Control", "no-cache");
try
{
        Map parameters = new HashMap();
        parameters.put("SUBREPORT_DIR", JasperReportFilepath);
        parameters.put("TestId", testID);

        JasperPrint jprint=JasperFillManager.fillReport(filePath, parameters, conn);

        byte[] output=JasperExportManager.exportReportToPdf(jprint);            
        System.out.println("Size====>"+output.length);

        servletOutputStream.write(output);
        servletOutputStream.flush();
        servletOutputStream.close();

        System.out.println("===============>Streaming perfectly");


}
catch(Exception e)
{
        System.out.println("===============>+JasperException"+e.getMessage());
}

我也无法得到任何错误信息。一切正常,但文件不流。 请帮我解决问题。

你最好用:

JasperExportManager.exportReportToPdfStream(jprint, servletOutputStream);

嘿,一天晚了,一美元短,但有没有人问他的HTML中是否有<OBJECT>标签?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM