File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes getting error while displaying PDF  from jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "getting error while displaying PDF  from jsp" Watch "getting error while displaying PDF  from jsp" New topic

getting error while displaying PDF from jsp

purushotham podaralla

Joined: Sep 12, 2007
Posts: 29

i am getting this error while displaying PDF from jsp. "Adobe reader could not open'report1.pdf' because it is either not a supported file type or because the file has been corrupted(for example, it was sent as an email attachment and wasn't correctly decoded".

below code used for this purpose.

byte byteArray[]="this is to generate the PDF file".getBytes();
logger.debug("********* byte array *********"+ byteArray);
response.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
response.setHeader("Cache-Control", "max-age=0");
response.setHeader("Content-disposition", "attachment; filename=Report1.pdf");
ServletOutputStream out;
try {
out = response.getOutputStream();
} catch (IOException e) {
// TODO Auto-generated catch block

actually i want to display pdf, excel, rpt, and xml. please let me know the issue in this code.

thanks in advance.

Wilmar Rodriguez
Ranch Hand

Joined: Jan 10, 2008
Posts: 53
Try this

ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
Dee Brown
Ranch Hand

Joined: Jun 14, 2008
Posts: 94
The content type should be "application/pdf". I would hardcode this for testing purposes. Is it possible the file was not found? Does File.length() return the correct file size? Lastly, here is a link to a working example:
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

Which framework are you using?

Some frameworks set the content type for you, so even if you set it to application/pdf, the mime type might get overridden. Check the headers of the actual response getting sent back to the client and see what's in there.

-Cameron McKenzie
purushotham podaralla

Joined: Sep 12, 2007
Posts: 29
hi Cameron McKenzie,

i am using struts frame work.
I agree. Here's the link:
subject: getting error while displaying PDF from jsp
It's not a secret anymore!