struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Niall Pemberton" <niall.pember...@blueyonder.co.uk>
Subject Re: Downloading a file from an Action class.
Date Thu, 10 Jun 2004 13:00:00 GMT
Miquel,

All you are doing is writing out a file on your server and then sending back
an empty response to the user - if you want to download the file, you need
to write it out to the response.

Try creating your file by writing it to a ByteArrayOutputStream, rather than
FileOutputStream

   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   PrintWriter pw = new PrintWriter(baos);
   .... etc etc

Then write that ByteArrayOutputStream to the response...

   ServletOutputStream sos = response.getOutputStream();
   baos.writeTo(sos);
   sos.flush();


Niall

----- Original Message ----- 
From: "Miquel Angel" <masegui@sidiem.com>
To: "Struts Users Mailing List" <user@struts.apache.org>
Sent: Thursday, June 10, 2004 1:01 PM
Subject: Downloading a file from an Action class.


> Hi!
>
> I try to download a file from an action class to the user PC. That's what
I
> do in the action class:
>
> String Path = "C:" + java.io.File.separator; //Location of the file
> String FileName =  Path + "CSV.TXT"; //Name of the file
> int buffer = 10 * 1024;
> File file = new File(FileName);
> FileOutputStream fos = new FileOutputStream(file);
>
> //Fill the file with some information.
> PrintWriter pw = new PrintWriter(fos);
> for (int i = 0; i< 200; i++) {
> pw.write("l1 campo1" + ";" + "l1 campo2" + ";" + "l1 campo3" + "\n");
> pw.write("l2 campo1" + ";" + "l2 campo2" + ";" + "l2 campo3" + "\n");
> }
>
> pw.flush();
> pw.close();
>
> fos.close();
>
> //download
> response.reset();
> response.setContentType("text/plain");
> response.setHeader("Content-Disposition", "attachment; filename=\"" +
> file.getName() + "\"");
> response.flushBuffer();
>
> return null;
>
> All works fine, but when the user save the file, it's empty. Any ideas..
>
> Thanks in advance.
>
> Miquel Angel Segui
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message