解决getOutputStream() has already been called for this response 悟寰轩

getOutputStream() has already been called for this response异常出现的原因和解决方法:

jsp中出现此错误一般都是在jsp中使用了输出流(如输出图片验证码,文件下载等),没有妥善处理好的原因。


具体的原因:jsp编译成servlet之后在函数

_jspService(HttpServletRequest request, HttpServletResponse response)

 

的最后
有一段这样的代码

Java代码  收藏代码
  1. finally {  
  2.       if (_jspxFactory != null)   
  3.           _jspxFactory.releasePageContext(_jspx_page_context);  
  4. }  

 

这里是在释放在jsp中使用的对象,会调用response.getWriter(),因为这个方法是和 response.getOutputStream()相冲突的!所以会出现以上这个异常。然后当然是要提出解决的办法,其实挺简单的,在使用完输出流以 后调用以下两行代码即可:

Java代码  收藏代码
    1. out.clear();  
    2. out = pageContext.pushBody(); 
原文地址:https://www.cnblogs.com/sunxucool/p/3094946.html