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();
  	 %>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章