才子佳人博客

我的故事我讲述

getOutputStream() has already been called异常解决办法
 
来源:xjh  编辑:xjh  2008-05-06

环境说明:linux,jsp,tomcat,mysql

异常错误:getOutputStream() has already been called for this response

原因分析:在tomcat下调用jsp文件,而在jsp文件中使用了输出流(如输出图片验证码,文件下载等),但是没有妥善处理好输出流,重新被调用。

具体的原因:在tomcat中jsp编译成servlet之后在函数_jspService(HttpServletRequest request, HttpServletResponse response)的最后
有一段这样的代码
finally {

if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);

}

这里是在释放在jsp中使用的对象,会调用response.getWriter(),因为这个方法是和
response.getOutputStream()相冲突的!所以会出现以上这个异常。

解决方法:在使用OutputStream输出流完成后,调用下面2个方法即可解决该问题:

out.clear();

out = pageContext.pushBody();

示例代码:

OutputStream os=response.getOutputStream();

os.write(new String("ddddd").getBytes());

os.flush();

os.close();

out.clear();

out = pageContext.pushBody();

参考:http://blog.csdn.net/shaokun305/archive/2007/11/16/1888345.aspx


分类:网络日志| 查看评论
相关文章
文章点击排行
本年度文章点击排行
发表评论:
  • 昵称: *
  • 邮箱: *
  • 网址:
  • 评论:(最多100字)
  • 验证码: