• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

opening a File within my WEB-INF folder

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

I have a file in my WEB-INF folder that I wish to open as a File object in my servlet. When I try...



...the path it tries to open is relative to the root of the OS file system, not my web application. So at the moment I open the file like this...



...needlesss to say that's rather ugly and unportable.

So, can anyone inform me how to open a File in my WEB-INF folder using a path that is relative to my context root?

Thanks!

John
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When specifying relative paths, the java.io API relies on the current working directory which is not per se uniform in all environments. A bad practice thus. You should always specify absolute file system paths.

Make use of ServletContext#getRealPath() to convert a relative web path to an absolute file system path and use it in java.io stuff.
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This doesn't look like a relative path to me ... Looks like an absolute path starting at root.
(Assuming you are on a Unix like system since where you root starts with a '/')
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The url given in File object will be with respect to your application server.You can access files under WEB-INF as servlet stream.You can google on the same.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bauke Scholtz wrote:When specifying relative paths, the java.io API relies on the current working directory which is not per se uniform in all environments. A bad practice thus. You should always specify absolute file system paths.

Make use of ServletContext#getRealPath() to convert a relative web path to an absolute file system path and use it in java.io stuff.



what he said
 
JohnWilliam Fitz
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sam Mercs said: >>"This doesn't look like a relative path to me ... Looks like an absolute path starting at root.
(Assuming you are on a Unix like system since where you root starts with a '/')"<<

Thanks for your reply Sam.

Actually I said "relative to my context root", not "relative path". Perhaps you are familiar with the use of ServletContext#getResource(String path), where the dox say "Returns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context root. "

Fortunately I got some useful replies from other users, and I am on my way. Thank you all for quick and helpful replies!! This forum is great.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As said, use ServletContext#getRealPath().
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I prefer getResourceAsStream rather than getRealPath, since the latte does work if your app is packaged in a WAR file
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I prefer getResourceAsStream rather than getRealPath


That only works if the path to the resource is covered by the classpath.
 
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic