You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Vernon Wu <ve...@gatewaytech.com> on 2002/07/19 19:40:48 UTC
How to find out of path of an application
In my current project, the user can upload his/her photos to the web server so that people can view these photos. I
create an image directory under webapps/myapp. When the user upload his/her photos at the first time, a new
directory under the image directory is created with the user' user ID.
In a Struts-like structure, req.getContextPath() + "/images" doesn't field a right path, but "myapp/images" in a Java
bean. How can I find the right path?
Thanks for your help.
Vernon
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: How to find out of path of an application
Posted by "Craig R. McClanahan" <cr...@apache.org>.
On Fri, 19 Jul 2002, Vernon Wu wrote:
> Date: Fri, 19 Jul 2002 17:26:46 -0700
> From: Vernon Wu <ve...@gatewaytech.com>
> Reply-To: Tomcat Users List <to...@jakarta.apache.org>,
> vernonw@gatewaytech.com
> To: tomcat-user@jakarta.apache.org
> Subject: Re: How to find out of path of an application
>
> Since no one would like help me out, or it is too simple question, I
> have to hacked out a soluation myself.
>
Your approach only works under the following conditions:
* Tomcat sets its working directory to CATALINA_HOME (depends
on how you start it)
* Webapp is deployed in the "webapps" subdirectory (not required)
The canonical approach to this is documented in the Javadocs for
ServletContext:
String saveDirectoryPathname =
getServletContext().getRealPath("/images");
However, the problem with this approach -- which has been discussed at
least three times in the last week on this mailing list (see the
archives), and untold numbers of times before -- is that you are *not*
guaranteed that there is such a thing as a "directory" that corresponds to
your webapp. It is perfectly legal for a servlet container to run a
webapp directly from a WAR file, or store it in some other internal format
that does not correspond to a filesystem. In such cases, getRealPath()
will return null.
A far safer thing is to pass the pathname of a directory to store images
into as a servlet initialization parameter, or something like that.
Craig
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: How to find out of path of an application
Posted by Vernon Wu <ve...@gatewaytech.com>.
Since no one would like help me out, or it is too simple question, I have to hacked out a soluation myself.
Here is what I find after more than a halp of hacking:
String saveDirectory = "webapps/" + req.getContextPath() + "/ images";
File f = new File(saveDirectory);
f.isDirectory() - No, f.exists() - No, f.canWrite() - No
But,
String saveDirectory = "webapps/" + req.getContextPath() + "/ images";
File f = new File(saveDirectory);
f = new File(f.getAbsolutePath());
f.isDirectory() - Yes, f.exists() - Yes, f.canWrite() - Yes
That doesn't sound right, does it? Why I need to get absolute path to be able write a file?
7/19/2002 10:40:48 AM, Vernon Wu <ve...@gatewaytech.com> wrote:
>
>In my current project, the user can upload his/her photos to the web server so that people can view these photos. I
>create an image directory under webapps/myapp. When the user upload his/her photos at the first time, a new
>directory under the image directory is created with the user' user ID.
>
>In a Struts-like structure, req.getContextPath() + "/images" doesn't field a right path, but "myapp/images" in a Java
>bean. How can I find the right path?
>
>Thanks for your help.
>
>Vernon
>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>