views:

178

answers:

2

Hi,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.

+1  A: 

You'd better use:

JasperExportManager.exportReportToPdfStream(jprint, servletOutputStream);
Bozho
Hi Bozho,Thanks for ur reply, I have tried this method too..But i could not see any streaming document.Can you please provide me some code snippet to stream a document using this logic
Kumar
what do you understand under "streamed". The file should just start downloading.
Bozho
ya..file need to download but i could not see any downloading..
Kumar
any exceptions?
Bozho
I could not get any exception ... I Print the size of the file in my program using byte[] output=JasperExportManager.exportReportToPdf(jprint); System.out.println("Size====>"+output.length);these two line..Its returning size as 768 or something depends on the file size.
Kumar
you may have some filters that hinder this from going to the client?
Bozho
A: 

Heh, day late and a dollar short, but did anyone ask if he had an <OBJECT> tag in his HTML?

Tony Ennis
I don't see why one would display a PDF file as an image.
BalusC
Yeah, the dangers of posting when searching for the same solution. Answer is improved.
Tony Ennis