Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpServletOutputStream doesnt seem to be working correctly on glassfish

 
Derick Potgieter
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic