Re: serving binary files

2005-12-28 Thread Emmanouil Batsis

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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


 




-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: serving binary files

2005-12-28 Thread Daniel Blumenthal
 

> -Original Message-
> From: news [mailto:[EMAIL PROTECTED] 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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: serving binary files

2005-12-27 Thread Laurie Harper

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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]