Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Unable to open PDF file from servlet.

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I am trying to open a PDF through a Servlet it is popping with an acrobat error with the message
"The file is damaged and could not be repaired"


I am using the following code to display the pdf file from the serlvet,

File pdfFile = new File(filePath);
OutputStream responseOutputStream = resp.getOutputStream();
resp.setContentType("application/pdf");
resp.setHeader("Content-disposition", "attachment;filename=\"" + file + "\"");
resp.setContentLength((int)filePath.length());
FileInputStream fileInputStream = new FileInputStream(pdfFile);
int size = fileInputStream.available();
byte[] content = new byte[size];
fileInputStream.read(content);
responseOutputStream.write(content);
responseOutputStream.flush();
fileInputStream.close();
responseOutputStream.close();


The same code works fine when the pdf contains only text content.
But it doesn't work when PDF contains text content with images.

Could anyone help me to solve this issue..
 
Sheriff
Posts: 22644
123
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kartik reddy wrote:fileInputStream.read(content);
responseOutputStream.write(content);


That's not the proper way to copy a file. InputStream.read(byte[]) can read fewer bytes than you intend. If that is the case the remainder of the byte[] will contain 0 bytes. This may very well be the cause of the corruption.
The solution is of course to use a loop:
 
kartik reddy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have used while loop, but still I could able to get this issue "The file is damaged and could not be repaired" when I am trying to open a PDF through a Servlet.

If you find any alternative solution to fix this issue please let me know.

Thanks in advance..
 
Rob Spoor
Sheriff
Posts: 22644
123
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens if you don't close responseOutputStream, but only flush it after writing the file?
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic