---
title: "display dynamic or static images"
author: "Allen.CD.China"
date: "2007-10-10T16:38:24Z"
tags: ["Apache", "HTML", "JSP", "Struts", "XML"]
slug: "display-dynamic-or-static-images"
draft: false
lastmod: "2007-10-10T16:38:24Z"
id: 130816
url: "https://allenj2ee.iteye.com/blog/130816"
---

How can we display dynamic or static images that can be provided as an array of bytes

[![](http://cwiki.apache.org/confluence/images/icons/notep_16.gif "Edit Page")](http://cwiki.apache.org/confluence/pages/editpage.action?pageId=14278) [Edit Page](http://cwiki.apache.org/confluence/pages/editpage.action?pageId=14278)   [![](http://cwiki.apache.org/confluence/images/icons/browse_space.gif "Browse Space")](http://cwiki.apache.org/confluence/pages/listpages.action?key=WW) [Browse Space](http://cwiki.apache.org/confluence/pages/listpages.action?key=WW)   [![](http://cwiki.apache.org/confluence/images/icons/add_page_16.gif "Add Page")](http://cwiki.apache.org/confluence/pages/createpage.action?spaceKey=WW&amp%3BfromPageId=14278) [Add Page](http://cwiki.apache.org/confluence/pages/createpage.action?spaceKey=WW&amp%3BfromPageId=14278)   [![](http://cwiki.apache.org/confluence/images/icons/add_blogentry_16.gif "Add News")](http://cwiki.apache.org/confluence/pages/createblogpost.action?spaceKey=WW&amp%3BfromPageId=14278) [Add News](http://cwiki.apache.org/confluence/pages/createblogpost.action?spaceKey=WW&amp%3BfromPageId=14278)

Added by [tm\_jee](http://cwiki.apache.org/confluence/users/viewuserprofile.action?username=tm_jee), last edited by [Ted Husted](http://struts.apache.org/2.x/~husted/index.html) on Jul 22, 2006  ([view change](http://cwiki.apache.org/confluence/pages/diffpages.action?pageId=14278&amp%3BoriginalId=17023))

To display an image represented as an array of bytes

- Reference an Action for the image src attribute
- Provide a helper method on the Action to return an array of bytes
- Provide a Result Type that renders the array to the response

## [Reference an Action]()

[**MyAction.jsp**  
\
`<img src="/myWebAppContext/myAction.do" />`]()

## []()[Provide Helper Methods]()

[**MyAction.java**  
\
`public class MyAction extends ActionSupport { public String doDefault() { return "myImageResult"; } public byte[] getMyImageInBytes() { .... } public String getMyContentType() { ... } public String getMyContentDisposition() { ... } public int getMyContentLength() { .... } public int getMyBufferSize() { ... } }`]()

## []()[Provide a Custom Result Type]()

[**struts.xml**  
\
`<struts> ... <result-types> <result-type name="myBytesResult" class="MyBytesResult" /> </result-types> ... <action name="myAction" class="MyAction"> <result name="myImageResult" type="myBytesResult"> <param name="contentType">${myContentType}</param> <param name="contentDisposition">${myContentDisposition}</param> <param name="contentLength">${myContentLength}</param> <param name="bufferSize">${myBufferSize}</param> <result> </action> ... </struts>`  
\
**MyBytesResult.java**  
\
`public class MyBytesResult implements Result { public void execute(ActionInvocation invocation) throws Exception { MyAction action = (MyAction) invocation.getAction(); HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType(action.getContentType()); response.setContentLength(action.getContentLength()); response.getOutputStream().write(action.getImageInBytes()); response.getOutputStream().flush(); } }` ` ` `From ： http://struts.apache.org/2.x/docs/how-can-we-display-dynamic-or-static-images-that-can-be-provided-as-an-array-of-bytes.html`]()

[]()