L Petre L Petre - 4 days ago 5
Java Question

Struts 2 display image

I am looking for a simpler solution to display an image inside a JSP page, within a Struts2 application. The solution which I found so far (and is working) is this one: Struts 2 dynamic image example.

However, writing the image as a stream of bytes seems to me like an overkill. Is there any other simpler solution? or it is absolutely necessary to use a stream of bytes?

Simply, writing

<img src="#image_location_on_disk">
is not an acceptable solution, because I want struts to be responsible for bringing my image (in my business logic I decide whether an image should be displayed or not)

Answer

If you want to stream the image for a location which is not publicly visible to the browser, then this is the standard way to do it.


In that case just use FileInputStream to read image, assign it to the inputsream and use stream result for the action.


In Struts2 you can perform the task of serving image with the stream result instead of writing directly to the response. You can also configure this result to the corresponding content type.

Comments