Jakarta Struts and File I/O

J

jazzdman

Hi,
I've been working on a webapp using Struts. I need to do some file
I/O in a few of my Action classes. Is there a way to do this easily?
What I mean is, is there an app(or server)-context relative way to read
files provided by Struts (or maybe even Tomcat)? A property I can read
or an object that does file I/O in a context relative way (ie something
like new File(mysteryObj.getServerDir()+"webbapps/my_app/foobar.txt")
instead of new File("/all/the/way/from/root/Tomcat
5.0/webapps/my_app/foobar.txt")) that I'm not aware of? The text books
I've read don't mention anything, and the Stuts website coughed up a
furball when I tried to search the Struts Users mailing list.


Thanks,

Jason Mazzotta

PS - Now that I think about it System.getProperty('user.dir') might be
a way to go, but is there a better way?
 
W

Wendy Smoak

I've been working on a webapp using Struts. I need to do some file
I/O in a few of my Action classes. Is there a way to do this easily?
What I mean is, is there an app(or server)-context relative way to read
files provided by Struts (or maybe even Tomcat)? A property I can read
or an object that does file I/O in a context relative way (ie something
like new File(mysteryObj.getServerDir()+"webbapps/my_app/foobar.txt")
instead of new File("/all/the/way/from/root/Tomcat
5.0/webapps/my_app/foobar.txt")) that I'm not aware of?

I use getResourceAsStream to read in .properties files. The files are
discovered relative to the classpath, so "just a filename" would be in
WEB-INF/classes. From there you can use dotted names like package names if
you want to place the files elsewhere. They could also be in a .jar file--
anywhere you could find a .class file.

Note that this isn't webapp specific, I do this in my data access layer
which works anywhere.
You can see it in action here:
http://www.pickwiki.com/cgi-bin/wiki.pl?FailoverUniSessionFactory

It gets more complicated if you need to write files from a webapp. The
Servlet container is required to provide you a place to put _temporary_
files, but past that you can't assume that there a filesystem exists, much
less that you can write to it. (Think of a .war file running without
expanding the files.) But that's why databases exist. ;)
The text books
I've read don't mention anything, and the Stuts website coughed up a
furball when I tried to search the Struts Users mailing list.

The struts-user list is archived at http://www.mail-archive.com and
http://marc.theaimsgroup.com
 
R

Ryan Stewart

Wendy Smoak said:
I use getResourceAsStream to read in .properties files. The files are
discovered relative to the classpath, so "just a filename" would be in
WEB-INF/classes. From there you can use dotted names like package names if
you want to place the files elsewhere. They could also be in a .jar file--
anywhere you could find a .class file.

Note that this isn't webapp specific, I do this in my data access layer
which works anywhere.
You can see it in action here:
http://www.pickwiki.com/cgi-bin/wiki.pl?FailoverUniSessionFactory

It gets more complicated if you need to write files from a webapp. The
Servlet container is required to provide you a place to put _temporary_
files, but past that you can't assume that there a filesystem exists, much
less that you can write to it. (Think of a .war file running without
expanding the files.) But that's why databases exist. ;)


The struts-user list is archived at http://www.mail-archive.com and
http://marc.theaimsgroup.com
Both of you should have a look at the javax.servlet.ServletContext.getResource
and javax.servlet.ServletContext.getResourceAsStream methods.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,084
Latest member
HansGeorgi

Latest Threads

Top