You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Dan Allen <da...@mojavelinux.com> on 2003/03/19 19:11:01 UTC

file path

Assuming I am storing a public file in my web directory who's real
path is

/var/tomcat/webapps/program/file.dat

How would I reference that file in my Action class in order to read
in the data?  My concern of course is how to get the path info
preceding my application, namely /var/tomcat/webapps/program/ 

Thanks!

Dan

-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Daniel Allen, <da...@mojavelinux.com>
http://www.mojavelinux.com/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
"The Linux philosophy is to laugh in face of danger.  Oops. 
Wrong one. 'Do it yourself' That's it" 
 -- Linus Torvalds
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: file path

Posted by Dan Allen <da...@mojavelinux.com>.
perhaps org.apache.commons.resources.file.web.WebappFileResources?

Dan

-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Daniel Allen, <da...@mojavelinux.com>
http://www.mojavelinux.com/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
"When you're raised by the Jesuits, you become either obedient 
or impertinent" 
 -- Jack McCoy, "Law and Order"
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: file path

Posted by Dan Allen <da...@mojavelinux.com>.
> There is absolutely no guarantee that webapp resources even *have* a "file
> path" at all -- it's perfectly legal for the container to run your
> application directly from a WAR file (Tomcat has an option for that), or
> dump your static resources into a database, or anywhere else it wants.
> 
> To read a resource file like this, use the following:
> 
>   InputStream is =
>     getServletContext().getResourceAsStream("/file.dat");
Yep, was actually cruising the sourcecode and just got this a few
minutes ago.  The servlet context has all the nice methods for file
access ;)

> where the path you pass starts with a slash, and is relative to the
> context root.  This is how Struts reads your struts-config.xml file, by
> the way, using a typical path like "/WEB-INF/struts-config.xml".
> 
> As a side note, placing your data file directly in the context root
> directory like this makes it possible for clients to access the file
> directly.  If you don't want this (in other words, if the file is only for
> internal use by your app), you can put it in /WEB-INF instead.
Naturally. I was actually writing a custom taglib to check for the
existence of an asset (graphic file, which is a headshot of a
member) and to use it as a logic:present like tag.  I am actually
going to start a new thread with the sourcecode for that.

Dan

-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Daniel Allen, <da...@mojavelinux.com>
http://www.mojavelinux.com/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
There is no such thing as a casual knowledge of xslt, either 
you know everything about it or you are sitting in the creek.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: file path

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Wed, 19 Mar 2003, Dan Allen wrote:

> Date: Wed, 19 Mar 2003 13:19:49 -0600
> From: Dan Allen <da...@mojavelinux.com>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: Struts-User List <st...@jakarta.apache.org>
> Subject: Re: file path
>
>
> Dan Allen (dan@mojavelinux.com) wrote:
>
> > Assuming I am storing a public file in my web directory who's real
> > path is
> >
> > /var/tomcat/webapps/program/file.dat
> >
> > How would I reference that file in my Action class in order to read
> > in the data?  My concern of course is how to get the path info
> > preceding my application, namely /var/tomcat/webapps/program/
> >
> > Thanks!
> >
> > Dan
>
> So even the struts-upload example doesn't get into this, because it
> allows you to type in a filename but it treats it as a full unix
> path, not a relative path.  How can my struts application work with
> files inside the current application.  Surely there must be a way to
> deal with real file paths of these files.

There is absolutely no guarantee that webapp resources even *have* a "file
path" at all -- it's perfectly legal for the container to run your
application directly from a WAR file (Tomcat has an option for that), or
dump your static resources into a database, or anywhere else it wants.

To read a resource file like this, use the following:

  InputStream is =
    getServletContext().getResourceAsStream("/file.dat");

where the path you pass starts with a slash, and is relative to the
context root.  This is how Struts reads your struts-config.xml file, by
the way, using a typical path like "/WEB-INF/struts-config.xml".

As a side note, placing your data file directly in the context root
directory like this makes it possible for clients to access the file
directly.  If you don't want this (in other words, if the file is only for
internal use by your app), you can put it in /WEB-INF instead.

>
> Dan

Craig

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: file path

Posted by Dan Allen <da...@mojavelinux.com>.
Dan Allen (dan@mojavelinux.com) wrote:

> Assuming I am storing a public file in my web directory who's real
> path is
> 
> /var/tomcat/webapps/program/file.dat
> 
> How would I reference that file in my Action class in order to read
> in the data?  My concern of course is how to get the path info
> preceding my application, namely /var/tomcat/webapps/program/ 
> 
> Thanks!
> 
> Dan

So even the struts-upload example doesn't get into this, because it
allows you to type in a filename but it treats it as a full unix
path, not a relative path.  How can my struts application work with
files inside the current application.  Surely there must be a way to
deal with real file paths of these files.

Dan

-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Daniel Allen, <da...@mojavelinux.com>
http://www.mojavelinux.com/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Since no one can ever know for certain whether or not his own 
view of creation is the correct one, it is impossible for him 
to know if someone else's is the wrong one.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org