aspose file tools*
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
Author

getting error while displaying PDF from jsp

purushotham podaralla
Greenhorn

Joined: Sep 12, 2007
Posts: 29
Hi

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.setContentLength(byteArray.length);
response.setContentType("application/"+rptFormat[--count]);
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();
out.write(byteArray);
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.getMessage();
}

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

thanks in advance.

regards
Purush
Wilmar Rodriguez
Ranch Hand

Joined: Jan 10, 2008
Posts: 53
Try this

response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
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:

http://www.herongyang.com/jsp/response_header_7.html
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
Greenhorn

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

i am using struts frame work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting error while displaying PDF from jsp
 
Similar Threads
Threads within Struts framework
When i try to open pdf, it shows 'There was an error opening this document.' message
Serving PDF with my servlet - performance & non-display
Response as PDF
Error Displaying PDF in JSP