You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jeffrey Winter <je...@attbi.com> on 2003/01/10 22:07:24 UTC
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?
Thanks.
Re: getCanonicalFile()
Posted by Jeffrey Winter <je...@attbi.com>.
> 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.
In my particular case the files I'm attempting to open are ones that are
being created and read by the application itself and are not deployed as
part of the web application.
I actually am passing in a directory name as a <context-param> though
this name has not path in it, it's just the flat name of the directory,
e.g., "somedir". This value is read in in the init() method, so when
I create the file path I do essentially:
File file = new File(directory + File.separator + "filename");
Granted, the absolute directory is relative to the context that Tomcat
itself is running in, but that is perfectly fine in my case (I would
think...).
My real issue is that I don't understand why
InputStream stream = new FileInputStream(file);
would be any different from
InputStream stream = new FileInputStream(file.getCanonicalFile());
When
file.getAbsolutePath(); and
file.getCanonicalFile().getAbsolutePath();
return the exactly the same string.
Thanks
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: getCanonicalFile()
Posted by "Craig R. McClanahan" <cr...@apache.org>.
On Fri, 10 Jan 2003, Jeffrey Winter wrote:
> Date: Fri, 10 Jan 2003 16:07:24 -0500
> From: Jeffrey Winter <je...@attbi.com>
> Reply-To: Tomcat Users List <to...@jakarta.apache.org>
> To: Tomcat Users List <to...@jakarta.apache.org>
> 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 =
getServletContext().getResourceAsStream("/filename");
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.
Craig
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>