wood burning stoves 2.0*
The moose likes Struts and the fly likes PDF file read Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "PDF file read" Watch "PDF file read" New topic
Author

PDF file read

Rahi Sanj
Greenhorn

Joined: Feb 13, 2003
Posts: 11
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.
michelle Wang
Ranch Hand

Joined: Jan 12, 2004
Posts: 65
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...


michelle Wang <br />SCJP 1.4
Rashmini Palakurti
Greenhorn

Joined: Jun 29, 2005
Posts: 13
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

Joined: Jan 12, 2004
Posts: 65
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

Joined: Jun 29, 2005
Posts: 13
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

Joined: Jan 12, 2004
Posts: 65
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PDF file read
 
Similar Threads
Serving PDF with my servlet - performance & non-display
PDF file read streaming
HTTP receiving problems with non text files.
Efficient Streaming Methods
Setting response header