Java Web報錯:getOutputStream() has already been called for this response解決方案


今天做了個導出excel表的功能。大概代碼如下:

ouputStream = response.getOutputStream();
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();
1
2
3
4
發現報錯

java.lang.IllegalStateException: getOutputStream() has already been called for this response
1
報錯原因
getOutputStream方法用於返回Servlet引擎創建的字節輸出流對象,Servlet程序可以按字節形式輸出響應正文。
getWriter方法用於返回Servlet引擎創建的字符輸出流對象,Servlet程序可以按字符形式輸出響應正文。
getOutputStream和getWriter這兩個方法互相排斥,調用了其中的任何一個方法后,就不能再調用另一方法。

解決方案有兩種:
在jsp頁面里清除response。
out.clear();
out = pageContext.pushBody(http://www.my516.com);
1
2
在controller層對應的方法上追加@ResponseBody。
---------------------


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM