This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Saving/opening a PDF file from the JSP Page

 
Sandesh Shashidhar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the PDF is inside the web application, why aren;t you just linking to it directly?
 
Sandesh Shashidhar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 547
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic