简体   繁体   中英

How to stream pdf document from servlet?

I am creating pdf document using jasper report and i need to stream that pdf document from servlet.Can anyone help me where i did mistake.This is the code snippet which i am using in my application.

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());
}

and i could not get any error message also.Everything is working fine but document is not streaming. Please help me to sort out the problem.

你最好用:

JasperExportManager.exportReportToPdfStream(jprint, servletOutputStream);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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