In this tutorial, we show you how to generate a dynamic image in Struts 2, via custom result type. For example,

<img src="<s:url action='ImageAction?imageId=darksouls.jpg' />" />

The value of imageId is the image name in your server path. In above case, the ImageAction class will output the image file based on the imageId paramater.

Tools and Struts used in this tutorial :

  1. Struts
  2. Eclipse 3.7
  3. Maven 3

1. Directory Structure

Directory structure of this tutorial. Review the image file path.

struts2 dynamic image directory structure

2. Action class

File : – A normal action, do nothing.

package com.mkyong.image.action;

import com.opensymphony.xwork2.ActionSupport;

public class DisplayAction extends ActionSupport  {


File : – core action class, to get the image based on the provided imageId parameter and convert it into bytes array.

package com.mkyong.image.action;

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;

public class ImageAction extends ActionSupport implements ServletRequestAware {

	byte[] imageInByte = null;
	String imageId;

	private HttpServletRequest servletRequest;

	public String getImageId() {
		return imageId;

	public void setImageId(String imageId) {
		this.imageId = imageId;

	public ImageAction() {

	public String execute() {
		return SUCCESS;

	public byte[] getCustomImageInBytes() {

		System.out.println("imageId" + imageId);

		BufferedImage originalImage;
		try {
			originalImage =;
			// convert BufferedImage to byte array
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ImageIO.write(originalImage, "jpg", baos);
			imageInByte = baos.toByteArray();
		} catch (IOException e) {
			// TODO Auto-generated catch block

		return imageInByte;

	private File getImageFile(String imageId) {
		String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
		File file = new File(filePath + "/Image/", imageId);
		return file;

	public String getCustomContentType() {
		return "image/jpeg";

	public String getCustomContentDisposition() {
		return "anyname.jpg";

	public void setServletRequest(HttpServletRequest request) {
		this.servletRequest = request;



3. Custom Result Type

A custom result type, and output the image to browser.

File :

package com.mkyong.image.result;

import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
import com.mkyong.image.action.ImageAction;

public class CustomImageBytesResult implements Result {

	public void execute(ActionInvocation invocation) throws Exception {
		ImageAction action = (ImageAction) invocation.getAction();
		HttpServletResponse response = ServletActionContext.getResponse();



4. Struts.xml

See how everything is linked together.

File : struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"


	<package name="image" namespace="/Image" extends="struts-default">

			<result-type name="imageResult"
				class="com.mkyong.image.result.CustomImageBytesResult" />

		<action name="Display" class="com.mkyong.image.action.DisplayAction">

		<action name="ImageAction" class="com.mkyong.image.action.ImageAction">
			<result name="success" type="imageResult">



5. JSP Page

A JSP page to display the dynamic image via ImageAction.

File : display.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
	<h1>Struts 2 Dynamic Image Example</h1>

	<img src=" <s:url action='ImageAction?imageId=darksouls.jpg' />" />


6. Demo

See output.


struts2 dynamic image demo

Download Source Code

Download It – (26kb)


  1. Struts2 display dynamic image as array of bytes
  2. image servlet example
  3. Struts2 URL tag example
  4. Convert image to bytes of array in Java
  5. Struts2 stream result example