Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Geoff Winsor - 07 Mar 2007 23:06 GMT
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
Lew - 08 Mar 2007 02:19 GMT
> Hi,
> I am developing an application using the Struts 1.2.9 framework (and
[quoted text clipped - 11 lines]
> 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
Ramita - 08 Mar 2007 04:32 GMT
> > Hi,
> > I am developing an application using the Struts 1.2.9 framework (and
[quoted text clipped - 17 lines]
>
> -- 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
NathanIEI - 08 Mar 2007 05:52 GMT
> Hi,
> I am developing an application using the Struts 1.2.9 framework (and
[quoted text clipped - 12 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.