This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Saving/opening a PDF file from the JSP Page 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 » Java » Servlets
Bookmark "Saving/opening a PDF file from the JSP Page" Watch "Saving/opening a PDF file from the JSP Page" New topic
Author

Saving/opening a PDF file from the JSP Page

Sandesh Shashidhar
Greenhorn

Joined: Feb 01, 2010
Posts: 2
I need help regarding the below procedure

I have a PDF file in my Webcontent/dir/helpdir.pdf and in JSP page i am trying to download the PDF by calling a servlet
The PDF is located in Webcontent/dir folder . But when i run the program i am getting filenot found. Please guide me . Below is the code snipet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
if (_logDebug) {
_log.debug(ENTERING + _className + ".doGet()");
}
OutputStream outStream = response.getOutputStream();
String fileName="helpDoc.pdf";
if (_logDebug) {
_log.debug("fileName"+fileName);
}
String filePath =request.getContextPath()+"//doc//";
_log.debug("File"+filePath);
File f=new File(filePath, fileName);
response.setContentType( "application/pdf" );
response.setContentLength((int)f.length());
response.setHeader("Content-Disposition","attachment; filename=\"helpDocument.pdf\"");
response.setHeader("Cache-Control", "max-age=60");
byte[] buf = new byte[50000];
FileInputStream inStream = new FileInputStream(f);
int sizeRead = 0;
while ((sizeRead = inStream.read(buf, 0, buf.length)) > 0) {
outStream.write(buf, 0, sizeRead);
}
inStream.close();
outStream.close();
}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

If the PDF is inside the web application, why aren;t you just linking to it directly?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sandesh Shashidhar
Greenhorn

Joined: Feb 01, 2010
Posts: 2
Since i need the pdf to display a popup asking do you want to open or save i am not linking it directly.
Yes my file is inside the webapp itself

Do i need to use getservletcontext.getrealpath()

If i am wrong can you please guide me
Rudy Gireyev
Ranch Hand

Joined: May 03, 2011
Posts: 39
Sandesh Shashidhar wrote:
I have a PDF file in my Webcontent/dir/helpdir.pdf and in JSP page i am trying to download the PDF by calling a servlet
The PDF is located in Webcontent/dir folder

Hello Sandesh. In your desription you state that the file is in the /dir directory .....
Sandesh Shashidhar wrote:String filePath =request.getContextPath()+"//doc//";
But in your code you are opening the /doc directory
Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 518
    
    2

As Bear pointed out, you can give a direct link to the pdf. But when you click that link, I guess browser would automatically open a pop up and asks you, if you want to save it or open it. Can you try that and see, if mere linking works for you, if not we can think of other options.


Regards
KumarRaja

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41035
    
  43
Sandesh Shashidhar wrote:Since i need the pdf to display a popup asking do you want to open or save i am not linking it directly.

Whether or not that dialog is shown is a browser setting. There's nothing the PDF can do about it, and very little (possibly nothing) the web app can do about it. You should accept that the user may have other ideas on how to work with files than what your web app would like him to have.


Ping & DNS - my free Android networking tools app
 
Don't get me started about those stupid light bulbs.
 
subject: Saving/opening a PDF file from the JSP Page
 
Similar Threads
Save Dialog in JSF Portlet
file downloader for image downloading not upto the limit....
error locating file in servlet
response.setHeader() not working in JSF portlet
unable to download xml file