aspose file tools*
The moose likes Struts and the fly likes OutputStream an image within struts2 action class got error - Response already committed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "OutputStream an image within struts2 action class got error - Response already committed" Watch "OutputStream an image within struts2 action class got error - Response already committed" New topic
Author

OutputStream an image within struts2 action class got error - Response already committed

Alex Winner
Greenhorn

Joined: Jun 01, 2011
Posts: 5
I got "[Servlet Error]-[Response already committed.]: java.lang.IllegalStateException: Response already committed." when I out stream an image in struts2 class:

public String execute() throws Exception {
FileInputStream fis = new FileInputStream(path);
ServletOutputStream out = response.getOutputStream();
response.setContentType(pathContentType);
byte[] rd = new byte[fis.available()];
fis.read(rd);
out.write(rd);
fis.close();
out.close();
if (hasErrors()) return INPUT;
else return SUCCESS;
}
....

Actually, I only need to output the image in the action, I don't know how to let struts2 action not forward to a result. so I left the result empty:
<action name="image_*" method="{1}" class="com.solution.trade.listing.Image">
<interceptor-ref name="fileUpload">
<param name="maximumSize">2097152</param>
<param name="allowedTypes">image/png,image/gif,image/jpeg,image/pjpeg,application/pdf</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="input">Image.jsp</result>
<result></result>
</action>

I debuged into the excute methods and found the error is not generated in excute methods, but after it returns. The image display fine, but just throw this annoying exception.

If anyone could provide tips on how to stream out a image within struts2 action, it is greately appreciated.


Souvvik Basu
Ranch Hand

Joined: Apr 05, 2010
Posts: 96
Hi Alex,
I'm not sure if this is the reason for this exception....but do you think you can forward the result to a jsp after you have written the output to a stream? I think you are not allowed to do that. You need to choose;either use a jsp to show the result, or write out to a stream.
Hope this helps.....
Alex Winner
Greenhorn

Joined: Jun 01, 2011
Posts: 5
Even I put a jsp as a forward result. I still get same exception. The image is displayed, the jsp is not display though.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OutputStream an image within struts2 action class got error - Response already committed