aspose file tools*
The moose likes Servlets and the fly likes opening a File within my WEB-INF folder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "opening a File within my WEB-INF folder" Watch "opening a File within my WEB-INF folder" New topic
Author

opening a File within my WEB-INF folder

JohnWilliam Fitz
Ranch Hand

Joined: Oct 13, 2008
Posts: 80
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
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605



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 '/')

Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Surya Kant
Ranch Hand

Joined: Mar 29, 2005
Posts: 104
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.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Oct 13, 2008
Posts: 80
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

Joined: Oct 08, 2006
Posts: 2458
As said, use ServletContext#getRealPath().
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I prefer getResourceAsStream rather than getRealPath, since the latte does work if your app is packaged in a WAR file
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
I prefer getResourceAsStream rather than getRealPath

That only works if the path to the resource is covered by the classpath.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: opening a File within my WEB-INF folder