Re: Opening/reading a file from a Struts application...

2003-07-29 Thread Stephan Wiesner
You can load Properties files like this:
 java.io.InputStream in = baseClass.getResourceAsStream(fileName);
 Properties props = new Properties();
 props.load(in);
I use this with my base classes, as the have to work outside my Tomcat, 
too, that's what base classes for, after all.
Have not tried but should work with other files as well.

Stephan

Davide Bruzzone wrote:

Greetings all...

I'm trying to load and read a text file (on the server's filesystem)
from within a Struts application. I'd like to be able to put the file in
the WEB-INF, or the WEB-INF/classes directory, but am having trouble
finding the file (i.e. I'm having trouble obtaining the path that I need
in order to open the file), and reading it...
Here's what my research has turned up:

// Something like this will return an InputStream. This is fine for
properties files
// since you can load a properties file by passing an InputStream to the
load method
// on a Properties object  
Thread.currentThread().getContextClassLoader().getResourceAsStream("my.p
roperties");

The problem with this is that the methods that I'm using to read the
contents of files takes either a string that represents the file name,
or a File object... The methods then calculate the file's length, read
the file into an array of bytes, and return the contents of the array in
various forms (i.e. An array of bytes, a string, etc.).
Does anyone have any suggestions about how best to go about doing this
(I could change the methods that read the contents of files into
strings, but I'm not sure exactly how to go about doing this).
Any help would be greatly appreciated...

Cheers...

Dave   

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


RE: Opening/reading a file from a Struts application...

2003-07-29 Thread Vijay Balakrishnan
Check this class Resource.java created by Jason Hunter of servlets.com fame
to locate a file on the classpath first and then class loader getResource().

Vijay



-Original Message-
From: Craig R. McClanahan [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, July 29, 2003 11:36 AM
To: Struts Users Mailing List
Subject: Re: Opening/reading a file from a Struts application...




On Tue, 29 Jul 2003, Davide Bruzzone wrote:

> Date: Tue, 29 Jul 2003 11:57:41 -0600
> From: Davide Bruzzone <[EMAIL PROTECTED]>
> Reply-To: Struts Users Mailing List <[EMAIL PROTECTED]>
> To: Struts Users Mailing List <[EMAIL PROTECTED]>
> Subject: Opening/reading a file from a Struts application...
>
> Greetings all...
>
> I'm trying to load and read a text file (on the server's filesystem) 
> from within a Struts application. I'd like to be able to put the file 
> in the WEB-INF, or the WEB-INF/classes directory, but am having 
> trouble finding the file (i.e. I'm having trouble obtaining the path 
> that I need in order to open the file), and reading it...
>
> Here's what my research has turned up:
>
> // Something like this will return an InputStream. This is fine for 
> properties files // since you can load a properties file by passing an 
> InputStream to the load method
> // on a Properties object
> Thread.currentThread().getContextClassLoader().getResourceAsStream("my.p
> roperties");
>

This only works on files visible to the class loader (i.e. files in
/WEB-INF/classes, or packaged in JARs inside /WEB-INF/lib).  To read general
resources in your webapp, try ServletContext.getResourceAsStream()
instead.  You pass it a context-relative path like "/WEB-INF/my.properties".

> The problem with this is that the methods that I'm using to read the 
> contents of files takes either a string that represents the file name, 
> or a File object... The methods then calculate the file's length, read 
> the file into an array of bytes, and return the contents of the array 
> in various forms (i.e. An array of bytes, a string, etc.).
>

That's not going to be portable, because you cannot assume that webapp
resources actually live in the filesystem - you might be running straight
from a WAR file, or in a container that puts all the static resources in
BLOBs in a database, or ...

If you really need file i/o access to this stuff, you should create an
initialization parameter pointing at the directory containing the relevant
files, and then access then outside your webapp hierarchy.

> Does anyone have any suggestions about how best to go about doing this 
> (I could change the methods that read the contents of files into 
> strings, but I'm not sure exactly how to go about doing this).
>
> Any help would be greatly appreciated...
>
> Cheers...
>
> Dave
>

Craig

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

RE: Opening/reading a file from a Struts application...

2003-07-29 Thread Dolf Starreveld
At 12:23 -0600 7/29/03, Davide Bruzzone spoke thusly:

Note that getRealPath("/WEB-INF/...") is allowed to fail (under the spec).
I know for a fact that in several containers, if your deploy as a WAR file,
it will indeed fail.
Here are two alternatives that will always work to access files in /WEB-INF:

1)
String path = "/WEB-INF/myfile.ext";
InputStream istream = servletContext.getResourceAsStream(path);
2)
String path = "/WEB-INF/myfile.ext";
URL url = servletContext.getResource(path);
URLConnection uconn = url.openConnection();
InputStream istream = uconn.getInputStream();
What remains to solve your problem is to refactor your code to accept 
an InputStream rather than a File. It appears that File is used to 
length()
can be called on it so the byte array can be properly allocated.
If the resource is truly a file, it appears the istream.available() will
do that job for you (although I have not tested this).


Thank you...

I'll give it a try...

Cheers...

Dave

-Original Message-
From: Jarnot Voytek Contr AU HQ/SC [mailto:[EMAIL PROTECTED]
Sent: Tuesday, July 29, 2003 12:02 PM
To: 'Struts Users Mailing List'
Subject: RE: Opening/reading a file from a Struts application...
-Original Message-
From: Jarnot Voytek Contr AU HQ/SC
Sent: Friday, June 27, 2003 11:53 AM
To: 'Struts Users Mailing List'
Subject: RE: getting a file path to /WEB-INF from a HttpSession
try

request.getSession(false).getServletContext().getRealPath("/WEB-INF/..."
))
or, in an action you could do

getServlet().getServletContext().getRealPath("/WEB-INF/...");

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--
Dolf Starreveld 190 Thompson Square
http://www.starreveld.com   Mountain View, CA 94043
Home: (650) 966-1404Mobile: (415) 613-7229
FAX:  (650) 967-2863
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: Opening/reading a file from a Struts application...

2003-07-29 Thread Craig R. McClanahan


On Tue, 29 Jul 2003, Davide Bruzzone wrote:

> Date: Tue, 29 Jul 2003 11:57:41 -0600
> From: Davide Bruzzone <[EMAIL PROTECTED]>
> Reply-To: Struts Users Mailing List <[EMAIL PROTECTED]>
> To: Struts Users Mailing List <[EMAIL PROTECTED]>
> Subject: Opening/reading a file from a Struts application...
>
> Greetings all...
>
> I'm trying to load and read a text file (on the server's filesystem)
> from within a Struts application. I'd like to be able to put the file in
> the WEB-INF, or the WEB-INF/classes directory, but am having trouble
> finding the file (i.e. I'm having trouble obtaining the path that I need
> in order to open the file), and reading it...
>
> Here's what my research has turned up:
>
> // Something like this will return an InputStream. This is fine for
> properties files
> // since you can load a properties file by passing an InputStream to the
> load method
> // on a Properties object
> Thread.currentThread().getContextClassLoader().getResourceAsStream("my.p
> roperties");
>

This only works on files visible to the class loader (i.e. files in
/WEB-INF/classes, or packaged in JARs inside /WEB-INF/lib).  To read
general resources in your webapp, try ServletContext.getResourceAsStream()
instead.  You pass it a context-relative path like
"/WEB-INF/my.properties".

> The problem with this is that the methods that I'm using to read the
> contents of files takes either a string that represents the file name,
> or a File object... The methods then calculate the file's length, read
> the file into an array of bytes, and return the contents of the array in
> various forms (i.e. An array of bytes, a string, etc.).
>

That's not going to be portable, because you cannot assume that webapp
resources actually live in the filesystem - you might be running straight
from a WAR file, or in a container that puts all the static resources in
BLOBs in a database, or ...

If you really need file i/o access to this stuff, you should create an
initialization parameter pointing at the directory containing the relevant
files, and then access then outside your webapp hierarchy.

> Does anyone have any suggestions about how best to go about doing this
> (I could change the methods that read the contents of files into
> strings, but I'm not sure exactly how to go about doing this).
>
> Any help would be greatly appreciated...
>
> Cheers...
>
> Dave
>

Craig

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: Opening/reading a file from a Struts application...

2003-07-29 Thread Davide Bruzzone
Thank you...

I'll give it a try...

Cheers...

Dave

-Original Message-
From: Jarnot Voytek Contr AU HQ/SC [mailto:[EMAIL PROTECTED]

Sent: Tuesday, July 29, 2003 12:02 PM
To: 'Struts Users Mailing List'
Subject: RE: Opening/reading a file from a Struts application...


-Original Message-
From: Jarnot Voytek Contr AU HQ/SC 
Sent: Friday, June 27, 2003 11:53 AM
To: 'Struts Users Mailing List'
Subject: RE: getting a file path to /WEB-INF from a HttpSession


try 

request.getSession(false).getServletContext().getRealPath("/WEB-INF/..."
))

or, in an action you could do

getServlet().getServletContext().getRealPath("/WEB-INF/...");

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: Opening/reading a file from a Struts application...

2003-07-29 Thread Jarnot Voytek Contr AU HQ/SC
-Original Message-
From: Jarnot Voytek Contr AU HQ/SC 
Sent: Friday, June 27, 2003 11:53 AM
To: 'Struts Users Mailing List'
Subject: RE: getting a file path to /WEB-INF from a HttpSession


try 

request.getSession(false).getServletContext().getRealPath("/WEB-INF/..."))

or, in an action you could do

getServlet().getServletContext().getRealPath("/WEB-INF/...");

--
Voytek Jarnot
Quidquid latine dictum sit, altum viditur.


> -Original Message-
> From: Davide Bruzzone [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, July 29, 2003 12:58 PM
> To: Struts Users Mailing List
> Subject: Opening/reading a file from a Struts application...
> 
> 
> Greetings all...
> 
> I'm trying to load and read a text file (on the server's filesystem)
> from within a Struts application. I'd like to be able to put 
> the file in
> the WEB-INF, or the WEB-INF/classes directory, but am having trouble
> finding the file (i.e. I'm having trouble obtaining the path 
> that I need
> in order to open the file), and reading it...
> 
> Here's what my research has turned up:
> 
> // Something like this will return an InputStream. This is fine for
> properties files
> // since you can load a properties file by passing an 
> InputStream to the
> load method
> // on a Properties object  
> Thread.currentThread().getContextClassLoader().getResourceAsSt
> ream("my.p
> roperties");
> 
> The problem with this is that the methods that I'm using to read the
> contents of files takes either a string that represents the file name,
> or a File object... The methods then calculate the file's length, read
> the file into an array of bytes, and return the contents of 
> the array in
> various forms (i.e. An array of bytes, a string, etc.).
> 
> Does anyone have any suggestions about how best to go about doing this
> (I could change the methods that read the contents of files into
> strings, but I'm not sure exactly how to go about doing this).
> 
> Any help would be greatly appreciated...
> 
> Cheers...
> 
> Dave   
> 
> -
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Opening/reading a file from a Struts application...

2003-07-29 Thread Davide Bruzzone
Greetings all...

I'm trying to load and read a text file (on the server's filesystem)
from within a Struts application. I'd like to be able to put the file in
the WEB-INF, or the WEB-INF/classes directory, but am having trouble
finding the file (i.e. I'm having trouble obtaining the path that I need
in order to open the file), and reading it...

Here's what my research has turned up:

// Something like this will return an InputStream. This is fine for
properties files
// since you can load a properties file by passing an InputStream to the
load method
// on a Properties object  
Thread.currentThread().getContextClassLoader().getResourceAsStream("my.p
roperties");

The problem with this is that the methods that I'm using to read the
contents of files takes either a string that represents the file name,
or a File object... The methods then calculate the file's length, read
the file into an array of bytes, and return the contents of the array in
various forms (i.e. An array of bytes, a string, etc.).

Does anyone have any suggestions about how best to go about doing this
(I could change the methods that read the contents of files into
strings, but I'm not sure exactly how to go about doing this).

Any help would be greatly appreciated...

Cheers...

Dave   

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]