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-2025 CODEPRJ.COM