常见的getOutputStream() has already been called for this response

异常原因:


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

解决方案:


1、在使用完输出流以后调用以下两行代码即可:

out.clear();  
out = pageContext.pushBody(); 

  将JSP页面的最后两行代码的注释去掉,这两行代码的作用如下:
  out.clear():清空缓存的内容。
  pageContext.pushBody():参考API

public BodyContent pushBody()
Return a new BodyContent object, save the current "out" JspWriter, and update the value of the "out" attribute in the page scope attribute namespace of the PageContext.

Returns:
the new BodyContent

·返回一个新的BodyContent(代表一个HTML页面的BODY部分内容)
·保存JspWriter实例的对象out
·更新PageContext的out属性的内容

2、将返回值设置为null
return null;
  之所以要写return null的原因在于: 每个方法都返回的是一个ActionForward对象,而response是ActionForward对象参数,既然已经调用了response就无需重复再去调用同一个response,所以就会使response冲突。当然原因也是因为在return 不为null时jsp中的内置对象out在JSP页面释放资源的时候调用了ServetResponse.getWriter()方法,那么就会与Response产生冲突。


原因追溯:
  就上述出现的问题进行分析研究,阅读了tomcat6的源代码发现,在调用response.getOutputStream()方法时会判断是否已调用了requonse.getWriter()方法;相反在调用requonse.getWriter()方法时会判断是否已调用了response.getOutputStream()方法。

在tomcat5时并没有出现这个问题,使用response.getOutputStream()方法可现实两种数据输出,只是在使用requonse.getWriter()时发生异常,而在tomcat6下则必须针对不同的数据类型选择相应输出流,这时为什么呢?仔细阅读tomcat6源代码没有发现问题的根源,给出的参考时:在一次客户端请求的响应动作中,只能调用一种响应输出方法,要么是getWriter()要么是getOutputStream(),且如果使用getOutputStream()方法输出字符串格式的数据时,中文无法正常通过将发生“java.io.CharConversionException:Not
an ISO 8859-1
character:”异常,在tomcat5下没有对getOutputStream()方法进行严格控制,中文字符串可正常通过。可见tomcat6的安全机制比tomcat5要严格,对于字符串格式的数据要求使用getWriter()方法输出响应,如果使用了getOutputStream()方法输出响应,则对输出的字符串数据进验证,要求高字节必须为0,显然中文是无法通过的。

参考地址:
http://blog.csdn.net/a9529lty/article/details/50630315
http://blog.csdn.net/yakoo5/article/details/4140500

当你在项目中进行数据导出到Excel时,你可能会遇到"getOutputStream() has already been called for this response"的异常。这个异常的原因可能是因为在你的代码中,同时调用了response.getOutputStream()和response.getWriter(),而根据Servlet规范,这两个方法不能同时调用。 在你提供的代码中,我可以看到你使用了@GetMapping注解来处理导出功能,并在方法参数中传入了HttpServletResponse对象。然后,你调用了ExcelPortUtil.excelPort()方法来进行数据写入Excel文件并返回。 为了解决"getOutputStream() has already been called for this response"异常,你可以按照下面的步骤进行调整: 1. 确保在你的代码中只调用response.getOutputStream()或response.getWriter()其中之一。 2. 检查你的ExcelPortUtil.excelPort()方法内部的实现,确保没有调用response.getOutputStream()或response.getWriter()。 3. 如果你的ExcelPortUtil.excelPort()方法内部确实需要使用response.getOutputStream(),那么你需要修改你的方法逻辑,确保在调用response.getOutputStream()之前没有调用response.getWriter()。 通过以上调整,你应该能够解决"getOutputStream() has already been called for this response"的异常。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [getOutputStream() has already been called for this response问题终极解决](https://blog.csdn.net/IT_51888_liang/article/details/122235797)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值