This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

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
Marshal
Posts: 67300
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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: 558
2
Hibernate Spring Java
  • 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: 43011
76
  • 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.
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!