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.
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);
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.
Joined: Jan 12, 2004
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.
Joined: Jun 29, 2005
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??
Joined: Jan 12, 2004
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...