Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

opening a File within my WEB-INF folder  RSS feed

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • 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
  • 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: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!