spring mvc 图片流输出到页面

spring mvc不像struts2:

 

<action name="TwoDimensionalCodeAction" class="com.wsg.action.TwoDimensionalCodeAction">
	<result type="stream">
               <param name="inputName">inputStream</param>  
	</result>
	<result name="testForm">/form_success.jsp</result>
</action>

 

直接设置结果既可实现跳转,显示页面。

 

在spring mvc的action中需要设置response,具体如下:

 

 

TwoDimensionalCode tdc =new TwoDimensionalCode();   
response.setContentType("image/jpeg");
ServletOutputStream op = response.getOutputStream();
tdc.getOutputStream().writeTo(op);
op.close();
response.flushBuffer();
System.out.println("二维码的值:"+tdc.getContext());

 tdc.getOutputStream()为二维码的二进制流。

这样选择相应的跳转,return null即可!

 

这样在把二进制流发送到页面时,response的流会出现一个问题:

threw exception [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
 

这个时候页面加上以下代码即可解决

 

 	<%
        out.clear();
        out=pageContext.pushBody();
  	 %>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值