You can do this without explicitly accessing the output stream of the HttpServletResponse.

1) In struts.xml define the result type of your action as a stream, you can define content type here aswell

      <action name="myAction" method="myMethod" class="myActionClass">
           <result type="stream">
               <param name="contentType">application/msword</param>
           </result>
       </action>

2) In your action class define a variable with name input stream and define a getter for it.

private InputStream inputStream;
   public InputStream getInputStream() {
       return inputStream;
   }

3) In your action method set the input stream to your file input stream

   File file = new File(downloadDirectory + filename);
   this.inputStream = new FileInputStream(file);

And that should work.

You can find more documentation on the stream result type here http://struts.apache.org/2.x/docs/stream-result.html

jneville wrote:
Hi,

I'm trying to code file download functionality in a Struts 2 action. The
action executes and the "success" result is invoked, but the pop-up window
in the browser, which prompts to save the file, does not appear.

I think I am configuring the response incorrectly (see code below). Any help
greatly appreciated.

Thanks,
John

My action code is as follows :-

        File file = new File(downloadDirectory + filename);
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);

        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("application/msword");
        response.setHeader("Content-disposition", "attachment; filename=" +
filename);

        OutputStream output = response.getOutputStream();
        byte[] buffer = new byte[4096];
        int count = 0;
        int n = 0;
        while (-1 != (n = bis.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }

        bis.close();
        fis.close();

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

Reply via email to