You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Brett G. Palmer" <pb...@qwest.net> on 2001/03/01 19:44:59 UTC

Reading a file from a Context

What is the best way to read a file from a Context in Tomcat?  I know I can
get the context path from the "getContextPath()" method, but most file
stream APIs require a full path.  There most be a clean way to accomplish
this without hard coding any directory path info.

Any suggestions would be appreciated?

Thanks,

Brett


Re: Reading a file from a Context

Posted by Nick Holloway <Ni...@pyrites.org.uk>.
pbganjanette@qwest.net (Brett G. Palmer) writes:
> What is the best way to read a file from a Context in Tomcat?  I know I can
> get the context path from the "getContextPath()" method, but most file
> stream APIs require a full path.  There most be a clean way to accomplish
> this without hard coding any directory path info.

I have used the following to read the contents of a file from another
context.

    ServletContext ctx = application.getContext( "/pages" );
    InputStream is = ctx.getResourceAsStream( "/agreement.txt" );

To read from another context (as I'm doing) does require that the context
has been set up as "crossContext".

By using "getResourceAsStream()", this should even work in containers
that don't unpack the WAR file to the filesystem.

-- 
 `O O'  | Nick.Holloway@pyrites.org.uk
// ^ \\ | http://www.pyrites.org.uk/

Re: Reading a file from a Context

Posted by Milt Epstein <me...@uiuc.edu>.
On Fri, 2 Mar 2001, Kevin Sangeelee wrote:

> On Thu, 1 Mar 2001, Brett G. Palmer wrote:
> 
> > What is the best way to read a file from a Context in Tomcat?  I know I can
> > get the context path from the "getContextPath()" method, but most file
> > stream APIs require a full path.  There most be a clean way to accomplish
> > this without hard coding any directory path info.
> 
> I've used the following in the past, where s is a ServletConfig object
> 
> 	ServletContext sc = s.getServletContext();
> 	String fullPath = sc.getRealPath("") + "WEB-INF/myfiles/xyz.dat"

Wouldn't getResource() (or getResourceAsStream()) be the way to go
with this?

Milt Epstein
Research Programmer
Software/Systems Development Group
Computing and Communications Services Office (CCSO)
University of Illinois at Urbana-Champaign (UIUC)
mepstein@uiuc.edu


Re: Reading a file from a Context

Posted by Kevin Sangeelee <ke...@susa.net>.
On Thu, 1 Mar 2001, Brett G. Palmer wrote:

> What is the best way to read a file from a Context in Tomcat?  I know I can
> get the context path from the "getContextPath()" method, but most file
> stream APIs require a full path.  There most be a clean way to accomplish
> this without hard coding any directory path info.

I've used the following in the past, where s is a ServletConfig object

	ServletContext sc = s.getServletContext();
	String fullPath = sc.getRealPath("") + "WEB-INF/myfiles/xyz.dat"

Kevin Sangeelee
Software Developer
Corpfin Ltd