You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@turbine.apache.org by Roel Derksen <r....@zx.nl> on 2001/11/27 11:37:30 UTC

file DOWNLOAD question.

Hi,

For my Turbine application I want to let the users download files from the
server from there own temp-storage.

I've quickly made that possible using the code below.

But for obvious reasons, this is not a perminate solution.
Also this example doesn't work for images like GIF and JPEG.

Is there a way to do this using the normal Turbine API, so I don't have to
write directly to the HttpServletResponse ?.


Greetings Roel Derksen


public class GetAttach extends SecureAction
{
 public void doPerform(RunData data, Context context)
 {
  doGetAttach(data,context);
 }

 public void doGetAttach(RunData data, Context context)
 {
  User user = data.getUser();
  String sFilename = data.getParameters().getString("filename");
  HttpServletResponse resp = data.getResponse();

  if(sFilename == null)
   return;

  File file = (File)user.getTemp(sFilename);

  file.length());
  if(!file.exists())
   return;

  FileInputStream istr = null;
  OutputStream ostr = null;

  resp.setContentType("application/download");
  resp.setContentLength((int)file.length());
  resp.setHeader("Content-Disposition", "attachment; filename=\"" +
file.getName() + "\";");
  try
  {
   istr = new FileInputStream(file);
      ostr = resp.getOutputStream();
      int curByte=-1;

      while( (curByte=istr.read()) !=-1)
       ostr.write(curByte);

      ostr.flush();
  }
  catch(Exception ex)
  {
     ex.printStackTrace(System.out);
  }
  finally
  {
   try
   {
    if(istr!=null)  {istr.close();System.out.print(" IN closed");}
          if(ostr!=null)  {ostr.close();System.out.print(" OUT closed");}
      }
      catch(Exception ex)
      {
    System.out.println("Major Error Releasing Streams: "+ex.toString());
      }
  }

 }

}


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>