display dynamic or static images

How can we display dynamic or static images that can be provided as an array of bytes
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 : 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值