• 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

PDF file read

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I am trying to read a pdf file from database. I want the pdf file to be opened streaming in the sense the file should open automatically.

I am getting the bytes properly. I am seeting the response content type, content length properly. I am including part of my code below.
... response.setContentType("application/pdf");
response.setContentLength(pdfBytes.length);
ServletOutputstream out = response.getOutputStream();
out.write(pdfBytes);
out.flush(); ....

When I try to write the bytes using ServletOutputstream I get the window's window option of saving or opening the file. When I select open option, I get message saying "there is an error reading the file.
First I have to save the file to system then open using pdf editor. Then file opens fine. While trying to save file, the default file extension is '.do'. I have to manually save as .pdf file. Is it because I am trying to response out from action?

But, if I read the pdf file from file system rather than from database, the pdf file is read streaming and the file is opened directly and properly.

I dont know what is the problem. Any valuable suggestions please?
Thank You.

Rahi S.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To solve this pbm, I saved the file's name into DB as well, so in the code:
get the file name as well:
String savePath = "attachment; filename=\"" + fileName.replace('\\', '_') + "\"";

then add in this line:
response.setHeader("Content-Disposition", savePath);

following are the same as urs:
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream servletOutputStream = response
.getOutputStream();
servletOutputStream.write(bytes);

you can have a try...
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!

We also have the same problem , i'll modify our code accordingly , but before that i want to know something from where did u write all this code , that is reading PDFBytes and setting it to outputStream..in an Action Class or Servlet??

In our project we did that in a servlet , but i want to move that code to an action class for Exception Handling.. but i couldnt do that..If u did it in Action class then let me know how U did that.
 
michelle Wang
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did all in action class,I didn't write any servlet class, bcs I unjar the struts jar package, and take out the action/ActionServlet.class and put it into my deploy folder. So I only use this servlet given by struts.
 
Rashmini Palakurti
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When i tried to do this in Action Class I dont know what's happening behind scenes if it is going into Infinite Loop or not able to write to ServletOutputStream , my system is hanged and PDF is not opening..

Can u tell me what the problem could be??
 
michelle Wang
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry I'm not sure what happened for your process, in this case, you have to look at the detail backend error output..But the code is working fine with me...
 
reply
    Bookmark Topic Watch Topic
  • New Topic