On Wed, 25 Apr 2001 [EMAIL PROTECTED] wrote:

> 
> I have a design question regarding file upload/download which is not
> completely Struts related but I was hoping someone could provide some
> suggestions.  I have succeeded with the upload using the struts upload
> package, and then I load the file into an Oracle BLOB field. Now I need to
> get it back to the user if they want to view it.
> 
> I was planning to create an Action for processing the file download. The
> user will click on a URL and pass in the ID of the file as a request
> parameter to the Action (i.e. fileDownload.do?id=1234). The Action will
> access a session EJB which then locates the requested file and returns it to
> the Action as a File object. Here is where I need advice: what is the best
> way to send this File back to the browser?  Can I change the response type
> to "file" somehow and stream this to the browser? Another option I thought
> about was saving the file to a temp directory and then redirecting the
> browser to this file. I would rather not write temp files if I don't have
> to. Or, is there any functionality provided with struts for sending a file
> back to the browser?
> 

This is one of the cases where you might actually want to have an Action
create the response itself, instead of forwarding to a JSP page to do
it.  The reason for this is that JSP pages aren't meant for binary output
(like a GIF image) -- but servlets can do this easily.

An example of an Action to do this might look like this:

  byte buffer[] = ... load byte array from the database ...
  int size = xxx; // Number of bytes to be sent

  response.setContentLength(size);
  response.setContentType("image/gif");  // Or whatever is correct
  ServletOutputStream stream = response.getOutputStream();
  int sent = 0;
  while (sent < size) {
    int count = stream.write(buffer, sent, size - sent);
    sent += count;
  }
  stream.flush();

  return (null);

Note that NULL is returned, instead of an ActionForward.  This is the
signal to the controller servlet that the Action has already created the
response, so no forwarding is required.


> Any suggestions greatly appreciated...
> 
> Thanks,
> Bob
> 

Craig


Reply via email to