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。
---------------------

猜你喜欢

转载自www.cnblogs.com/hyhy904/p/11082211.html
今日推荐