• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getting error while displaying PDF from jsp

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this

response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Cameron McKenzie,

i am using struts frame work.
 
You can thank my dental hygienist for my untimely aliveness. So tiny:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic