display dynamic or static images
How can we display dynamic or static images that can be provided as an array of bytes
Edit Page
Browse Space
Add Page
Add News
Added by tm_jee, last edited by Ted Husted on Jul 22, 2006 (view change)
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
我也用这个做了一个MyBytesResult但是报OutputStream已经被call了 我是这么做的我的一个java类里有多个action,其中一个action处理图片,其它处理数据 程序是先请求一个action然后返回jsp页面,在该页面上有个 但是总是报response.getOutputStream()已经被调用了