tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <>
Subject Re: getCanonicalFile()
Date Fri, 10 Jan 2003 21:54:04 GMT

On Fri, 10 Jan 2003, Jeffrey Winter wrote:

> Date: Fri, 10 Jan 2003 16:07:24 -0500
> From: Jeffrey Winter <>
> Reply-To: Tomcat Users List <>
> To: Tomcat Users List <>
> Subject: getCanonicalFile()
> I just upgraded from Tomcat 4.0 to the latest Tomcat 4.1 and
> noticed something very strange that I was hoping someone could
> give me some insight on:
> Everyplace that I use to get a FileInputStream by simply saying:
>    File file = new File("filename");
>    InputStream istream = new FileInputStream(file);
> I've had to change to
>    File file = new File("filename");
>    File file2 = file.getCanonicalFile();
>    InputStream istream = new FileInputStream(file2);
> In the second case calling
>     file.getCanonicalPath()   or
>     file2.getCanonicalPath()
> returns the same string.
> I get the sense that there is something fundamental I'm
> just forgetting or not thinking about.  Any ideas?

One fundamental thing you are doing is assuming that the current working
directory of the servlet container will never change.  That's a very
dubious assumption, and will likely mess up programs that depend on it
even with the same version of the container (4.1 installed on Linux versus
Windows, or installed as a service versus run standalone).

If you are opening a resource that is inside your webapp, you should use
ServletContext.getResource().  For example, if "filename" is in the
top-level directory of your webapp, open it like this:

  InputStream istream =

As an extra added bonus, this works even if you deploy your app on a
server that does not unpack WAR files.

If you need to reference files in some arbitrary directory someplace, you
should pass the pathname of that directory to your servlet as an init
parameter, and then use that path to construct an absolute path to the
file you want.

> Thanks.


To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message