This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Other Java Products and Servers and the fly likes HttpServletOutputStream doesnt seem to be working correctly on glassfish Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Products » Other Java Products and Servers
Bookmark "HttpServletOutputStream doesnt seem to be working correctly on glassfish" Watch "HttpServletOutputStream doesnt seem to be working correctly on glassfish" New topic
Author

HttpServletOutputStream doesnt seem to be working correctly on glassfish

Derick Potgieter
Ranch Hand

Joined: Feb 19, 2004
Posts: 53
Hi All,

Please can anyone explain to me why this doesnt work on glassfish but runs perfectly on 8.x

byte[] bytes = null;

bytes = JasperRunManager.runReportToPdf(jasperReport, parameters, getConnection());

resp.setContentType("application/pdf");
resp.setHeader("Content-Disposition","attachment; filename=report.pdf");
resp.setContentLength(bytes.length);

//This generates a 23kb file...that doesnt display data in acrobat, 19blank pages
BufferedOutputStream bos = new BufferedOutputStream(resp.getOutputStream());
bos.write(bytes,0,bytes.length);
bos.flush();
bos.close();

//This generates a 23kb file...that displays the correct data in acrobat, 19 pages with data
BufferedOutputStream bosFile = new BufferedOutputStream(new FileOutputStream("c:\\test3.pdf"));
bosFile.write(bytes,0,bytes.length);
bosFile.flush();
bosFile.close();

The thing that bugs me is that the files are different when i view them thought wordpad.
The one gets send to the browser, the other to my c drive. But as you can see, they are produced with the same byte[].

Please anyone...i`m going nuts.

Rgds
Derick


SCJP, SCJD, SCWCD, SCBCD (JEE)
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
What happens when you try using the below mentioned code ?

int size = bytes.length;

resp.setContentType("application/pdf");
resp.setContentLength(size);
resp.setBufferSize(size);
resp.getOutputStream().write(bytes);


Rahul Bhattacharjee
LinkedIn - Blog
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
I don't know what is glassfish and neither I know what is 8.x.

What I think is happening is resp.getOutputStream() is somewhere using byte for its implementation because of which some characters are mixed up. It should be chars above 127. Try using an ASCII file (ant .txt/.rtf file) and see whether it opens up. You can also try using a hex editior/comparator to see which chars are getting mixed up.

Didn't read the whole post clearly. Using a byte[] array might also be a problem. But I have no clue why it is working in one and not in the other case.
[ November 14, 2006: Message edited by: Anupam Sinha ]
Derick Potgieter
Ranch Hand

Joined: Feb 19, 2004
Posts: 53
Thanks for all the reply`s.

Rahul:
I`ve copied your code...still nothing.

Anupam Sinha:
Glassfish is the latest version of the Sun Java Application Server.
And i must agree...that is sort of what i think is happening to...the files (one on harddrive and one thought browser) have a slight difference on certain characters...it seems like the encoding is screwing this up.

if i do a request.getRequestDispatcher("/reports/generated/myfile.pdf") it also displays the file as blank...but going strait to the location on the machine the file works....so it really looks like the way the file is being streamed to the browser.

Any other ideas?
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
One more thing I would certainly like you to try ;-)


String contentType = "application/pdf; " + "charset=" + System.getProperty(file.encoding);

resp.setContentType(contentType);

Please let me know if this works.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Sorry I posted the above post before completing that.

I have specified the encoding of the bytes along with the MIME.So that the browser gets to know the encoding of the bytes that is there in the response.

If this also doesn't work then try using tool like tcpmon to see whether the servlets is sending anything or not to the browser at all.

Hope this helps ;-)
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Rahul Bhattacharjee:

String contentType = "application/pdf; " + "charset=" + System.getProperty(file.encoding);



Please replace

String contentType = "application/pdf; " + "charset=" + System.getProperty(file.encoding);

with

String contentType = "application/pdf; " + "charset=" + System.getProperty("file.encoding");
Derick Potgieter
Ranch Hand

Joined: Feb 19, 2004
Posts: 53
Ok....i seem to be narrowing the scope of the problem..

I`ve tried to include the character set...but still nothing. It seems as it might have something to do with the JSF1.2 api`s inlcuded in glassfish.

I`m writing a test app to see and check. Will let you guys know.

Rgds
Derick
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HttpServletOutputStream doesnt seem to be working correctly on glassfish