How to create and write to a text file in Struts application

G

Geoff Winsor

Hi,
I am developing an application using the Struts 1.2.9 framework (and
Tomcat 5.5 as web server) in which I need to dynamically create a text
file that can be downloaded by the user.

The application (myApplication) is one many Struts applications under
the webapps directory.

I had planned to call a Struts Action that creates another object to
query a database and write the results to a text file in a /temp
directory. How do I do this without giving the absolute path?

I am not sure what keywords to use in order to search for the solution
on my own. If someone has some suggestions, including where I could look
this up, I would appreciate it very much.
Geoff
 
L

Lew

Geoff said:
Hi,
I am developing an application using the Struts 1.2.9 framework (and
Tomcat 5.5 as web server) in which I need to dynamically create a text
file that can be downloaded by the user.

The application (myApplication) is one many Struts applications under
the webapps directory.

I had planned to call a Struts Action that creates another object to
query a database and write the results to a text file in a /temp
directory. How do I do this without giving the absolute path?

I am not sure what keywords to use in order to search for the solution
on my own. If someone has some suggestions, including where I could look
this up, I would appreciate it very much.

Why not put the file(s) in a directory relative to your application context?
Put the directory below WEB-INF/ and it is only accessible to your code, and
via a relative path at that.

-- Lew
 
R

Ramita

Why not put the file(s) in a directory relative to your application context?
Put the directory below WEB-INF/ and it is only accessible to your code, and
via a relative path at that.

-- Lew


You may Try this ...

Create a folder under "webapps" directory of your application...and
give a relative path like this "folder/abc.txt"...
I hope it should work...

---Ramita
 
N

NathanIEI

Hi,
I am developing an application using the Struts 1.2.9 framework (and
Tomcat 5.5 as web server) in which I need to dynamically create a text
file that can be downloaded by the user.

The application (myApplication) is one many Struts applications under
the webapps directory.

I had planned to call a Struts Action that creates another object to
query a database and write the results to a text file in a /temp
directory. How do I do this without giving the absolute path?

I am not sure what keywords to use in order to search for the solution
on my own. If someone has some suggestions, including where I could look
this up, I would appreciate it very much.
Geoff

I'm not sure how well this will work with what you are doing, but you
could always create the file, as you are, and stream it back to the
user in the response, maybe using a generic servlet. A sloppy-quick
sample:

<code>

//get the file from disk, or use one you've created
File file = new File(file_you_created);
if(file.exist())
{
//HttpServletResponse
response.reset();
response.setContentType("text/plain");

//set the header, I think it's response.setHeader(foo,bar,etc)

//stream it out
byte[] buf = new byte[1 * 1024];
int nRead = 0;
FileInputStream fis = new FileInputStream(file);
while ((nRead = fis.read(buf)) != -1) {
response.getOutputStream().write(buf, 0, nRead);
}

//keep it clean
fis.close()
response.getOutputStream().flush();
response.getOutputStream().close();
}

</code>

or something like that...

Hope that helps,
Nate.
 

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,774
Messages
2,569,596
Members
45,130
Latest member
MitchellTe
Top