You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Daniel Blumenthal <da...@wordchamp.com> on 2005/12/24 20:06:18 UTC

serving binary files

Hi there, and happy holidays!
 
I have an application in which there are certain files which are only
accessible by certain users.  So, when a request comes to my "GetFile"
Action, I first verify that they have the correct permissions, then send the
file by opening the file and reading it into the response output stream
(code included below).  This works, but seems unbelievably hacky, and I was
wondering if there were a better way to do this.

Thanks!

Daniel

the code:
 
   response.reset();
   response.setContentType(mimeType);

   File f = new File(path);   
   long filelen = f.length();
   response.setContentLength((int)filelen);
   
   FileInputStream fileIn = new FileInputStream(f);
   BufferedInputStream bufIn = new BufferedInputStream(fileIn);
   BufferedOutputStream out = new
BufferedOutputStream(response.getOutputStream());
   
   final int READ_SIZE = 1024;
   int count;
   byte[] buffer = new byte[READ_SIZE];
   while ((count = bufIn.read(buffer,0,READ_SIZE)) != -1)
     out.write(buffer,0,count);
   
   out.flush();
   out.close();
   
   response.flushBuffer();

 



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


Re: serving binary files

Posted by Emmanouil Batsis <Em...@eurodyn.com>.
Daniel Blumenthal wrote:

>Hi there, and happy holidays!
>  
>

U 2.

I usually have an action pick up the binary stream (and related info), 
put it in the request scope and then forward to a servlet responsible 
for building the response using that info from the request context. 
Never liked actions that actually produce the request.

hth,

Manos


> 
>I have an application in which there are certain files which are only
>accessible by certain users.  So, when a request comes to my "GetFile"
>Action, I first verify that they have the correct permissions, then send the
>file by opening the file and reading it into the response output stream
>(code included below).  This works, but seems unbelievably hacky, and I was
>wondering if there were a better way to do this.
>
>Thanks!
>
>Daniel
>
>the code:
> 
>   response.reset();
>   response.setContentType(mimeType);
>
>   File f = new File(path);   
>   long filelen = f.length();
>   response.setContentLength((int)filelen);
>   
>   FileInputStream fileIn = new FileInputStream(f);
>   BufferedInputStream bufIn = new BufferedInputStream(fileIn);
>   BufferedOutputStream out = new
>BufferedOutputStream(response.getOutputStream());
>   
>   final int READ_SIZE = 1024;
>   int count;
>   byte[] buffer = new byte[READ_SIZE];
>   while ((count = bufIn.read(buffer,0,READ_SIZE)) != -1)
>     out.write(buffer,0,count);
>   
>   out.flush();
>   out.close();
>   
>   response.flushBuffer();
>
> 
>
>
>
>---------------------------------------------------------------------
>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


RE: serving binary files

Posted by Daniel Blumenthal <da...@wordchamp.com>.
 

> -----Original Message-----
> From: news [mailto:news@sea.gmane.org] On Behalf Of Laurie Harper
> Sent: Wednesday, December 28, 2005 12:28 AM
> To: user@struts.apache.org
> Subject: Re: serving binary files
> 
> Daniel Blumenthal wrote:
> > Hi there, and happy holidays!
> >  
> > I have an application in which there are certain files 
> which are only 
> > accessible by certain users.  So, when a request comes to 
> my "GetFile"
> > Action, I first verify that they have the correct permissions, then 
> > send the file by opening the file and reading it into the response 
> > output stream (code included below).  This works, but seems 
> > unbelievably hacky, and I was wondering if there were a 
> better way to do this.
> 
> What you have looks pretty standard. Make sure your action 
> returns null so Struts knows the response is complete, and 
> you should be fine.
> 
> L.

Thanks for the reply!  I was hoping you would tell me that there was a way
to create some kind of forward (or set something in the response) that would
tell Tomcat to simply return the correct file.  But, if this is the only
way, then it's nice not to have to worry about whether or not I'm doing it
right.

Best,
Daniel




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


Re: serving binary files

Posted by Laurie Harper <la...@holoweb.net>.
Daniel Blumenthal wrote:
> Hi there, and happy holidays!
>  
> I have an application in which there are certain files which are only
> accessible by certain users.  So, when a request comes to my "GetFile"
> Action, I first verify that they have the correct permissions, then send the
> file by opening the file and reading it into the response output stream
> (code included below).  This works, but seems unbelievably hacky, and I was
> wondering if there were a better way to do this.
> 
> Thanks!
> 
> Daniel
> 
> the code:
>  
>    response.reset();
>    response.setContentType(mimeType);
> 
>    File f = new File(path);   
>    long filelen = f.length();
>    response.setContentLength((int)filelen);
>    
>    FileInputStream fileIn = new FileInputStream(f);
>    BufferedInputStream bufIn = new BufferedInputStream(fileIn);
>    BufferedOutputStream out = new
> BufferedOutputStream(response.getOutputStream());
>    
>    final int READ_SIZE = 1024;
>    int count;
>    byte[] buffer = new byte[READ_SIZE];
>    while ((count = bufIn.read(buffer,0,READ_SIZE)) != -1)
>      out.write(buffer,0,count);
>    
>    out.flush();
>    out.close();
>    
>    response.flushBuffer();

What you have looks pretty standard. Make sure your action returns null 
so Struts knows the response is complete, and you should be fine.

L.


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