• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Alex Winner
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even I put a jsp as a forward result. I still get same exception. The image is displayed, the jsp is not display though.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic