aspose file tools*
The moose likes Struts and the fly likes Setting header and fetching OutputStream from Action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Setting header and fetching OutputStream from Action" Watch "Setting header and fetching OutputStream from Action" New topic
Author

Setting header and fetching OutputStream from Action

Albert Gan
Greenhorn

Joined: Aug 19, 2003
Posts: 15
Hi, i really need some help here.
I want to have the user download a file while the button gets clicked, and the file is generated in runtime.
I wrote a simple Action class to test this. Here's what's inside the perform() method :
String strngContent = "asdfasdfasdfasdf";
// this header is required for the download dialog from the browser to pop up
response.setHeader("Content-Disposition", "attachment; filename=test.download");
response.getOutputStream().write(strngContent.getBytes());
return new ActionForward(mapping.getInput());
And this is the error message that i got :
org.apache.jasper.JasperException: getOutputStream() has already been called for this response
I wonder if there is any work around for this problem in Struts ? I mean, i've been looking in the Action and RequestProcessor source, and i couldnt find any clue.
Please help, thanks in advance.
Yoo-Jin Lee
Ranch Hand

Joined: Nov 01, 2000
Posts: 119
Hi Albert,
It works for me with Struts 1.1 and Tomcat 4.1. What version of Struts are you using. It looks like 1.0.
Try 1.1.
And remember to call flush() on your Outputstream.

-Yoo-Jin
[ October 15, 2003: Message edited by: Yoo-Jin Lee ]
[ October 15, 2003: Message edited by: Yoo-Jin Lee ]
Yoo-Jin Lee
Ranch Hand

Joined: Nov 01, 2000
Posts: 119
I just looked at getOutputStream and getWriter they both throw an java.lang.IllegalStateException.
---
java.lang.IllegalStateException - if the getOutputStream method has already been called for this response object
---
If you are using Struts 1.0 then there could be a bug or else both getOutputStream() and getWriter() are being called somewhere.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
The immediate problem you're running into is that you cannot get both an OutputStream and an Writer for the same response.
The underlying problem is that you are trying to generate a response in two different places. This does not look healthy. Either your Action generates the response, and you do not return an ActionForward but simply return null to indicate that you have already generated the response. Or the view generates the response -- the usual approach -- in which case the Action has no business retrieving an OutputStream from the response object.
You seem to be doing both at the same time, this is asking for trouble.
- Peter
Albert Gan
Greenhorn

Joined: Aug 19, 2003
Posts: 15
Hello again.
Peter, i didnt intend to generate html output from my Action class. I just want to send a file so that the client could have a popup download dialog, and save the file.
Lee, thanks alot. guess what's wrong with my code ! i just miss the .flush() and i get the error message that the outputstream object has already been called twice. it's just a misleading error message, since in my code, i never call getOutputStream or getWriter twice .. or both.
Oh yeah, for those who need the popup download dialog to show up, you should set the Content-Length header with the size of the file and the Content-Type header to 'application/octet-stream', and the Content-Disposition to 'attachment; filename=<your filename>'
Thank you thank you thank you ....

[ October 15, 2003: Message edited by: Albert Gan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting header and fetching OutputStream from Action