• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Problem displaying pdf

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I need to display some pdf files which are sitting in a sub-folder inside WEB-INF. The path to the pdf is passed as a parameter from a jsp. The problem I am having is that instead of looking at the context path of my web-app, servlet is trying to load the pdf from the absolute path. For example, if the I pass the path /WEB-INF/HTMLHelp/common/sample.pdf, it goes to my hard drive and tries to look up
c: \WEB-INF\HTMLHelp\common\sample.pdf instead of going to the root of my application. Please let me know what I am doing wrong. Thanks in advance
 
Sheriff
Posts: 67641
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


String filename =(String)request.getParameter("url");
...
File pdf= new File(filename);



It's doing exactly what you are telling it to do. What would make you think that the File object would somehow magically detect that you want to make the filename you pass to it relative to another folder?

You need to tell the File object the complete path to the file you want to open.

Hint: check out the ServletContext.getRealPath() method.
[ July 25, 2006: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Sheriff
Posts: 67641
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S.

String filename =(String)request.getParameter("url");



Why are you bothering to cast a String to a String?
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a common mistake I find myself making every now and again. I forget that getParameter returns a String and not an Object.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're storing the files under WEB-INF (or anwhere within your webapp's directory structure) you shouldn't be using absolute file paths.
Look at ServletContext.getResourceAsStream.

Depending on how your app is deployed, getRealPath may or may not return a null value.
reply
    Bookmark Topic Watch Topic
  • New Topic