users@glassfish.java.net

Glassfish + streaming a pdf (or any file) to a page via a servlet

From: <derick.potgieter_at_gmail.com>
Date: Tue, 14 Nov 2006 00:12:13 -0500

Hi All,

I`m getting a problem on glassfish that I didn`t have on SJAS8.2 My reports i`m generating via Jasper used to work perfect on SJAS8.2, but on glassfish i get the same number of pages, just blank. So on my one report I get 19 pages of blank data.

But if i stream to a file it works perfectly. I even added code to write to a tmp file first...and even that file has the correct data in it, but as soon as I stream it to the ServletOutputStream....i get nothing.
(This is the same for .xml and .csv).

The code above is my 3rd attempt at changing the way it buffers and writes.
I`ve also used a byte[] to write into and then stream to the resp (the cleanest way I know) And also reading the file generated and streaming that out...nothing works.

As I said, the file produced in step on works to disk....just not to screen.(i`ve also swapped them around...writing to output stream first, and then output file)

Code:
      BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("c:\\test2.pdf"));
      
        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
      baos.write(JasperRunManager.runReportToPdf(jasperReport,
parameters, getConnection()));

      //writing to file...works perfectly
      baos.writeTo(bos);
      bos.flush();
      bos.close();
      
      //writing same byte array to output...doesnt work...well i get a
blank file
        ServletOutputStream sos = resp.getOutputStream();
      resp.setContentType("application/pdf");
      resp.setContentLength(baos.toByteArray().length);
      
      baos.writeTo(sos);
        sos.flush();
        sos.close();

Any ideas.

Rgds
Derick