getOutputStream() has already been called for this response
错误日志里偶尔会有getOutputStream() has already been called for this response这个错误
最近发现了高概率复现条件,所以顺手解决了一下:
首先根据这个错误关键信息,得知是错误产生原因是response.getWriter()和response.getOutputStream()等接口在调用时发生了资源占用
然而事实上在这个项目中并没有使用response.getWriter()和response.getOutputStream(),那么就需要更深入的去查找错误的原因
首先高概率复现条件是在进行redis操作的时候,这两个接口是进行流输出的接口,根据关键字查找,从redis相关操作中发现了一行序列化操作有进行流相关的操作。
public static byte[] serialize(Object object) throws Exception {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
// 序列化
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
byte[] bytes = baos.toByteArray();
return bytes;
} catch (Exception e) {
throw e;
}
}
八成就是这里没有close导致的bug了。再往深了看:
其中ByteArrayOutputStream用于捕获内存缓冲区的数据,转换成字节数组,但有趣的是对一个ByteArrayOutputStream进行close()操作没有任何效果,而且这样写不会产生重复关闭导致的Exception。
ObjectOutputStream用于进行序列化,这个没有close应该就是罪魁祸首了,但保险起见,还是两个操作都加上Close()
修改后代码如下:
public static byte[] serialize(Object object) throws Exception {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
// 序列化
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
byte[] bytes = baos.toByteArray();
oss.close();
baos.close();
return bytes;
} catch (Exception e) {
throw e;
}
finally {
if(oos != null){
oos.close();
}
if(baos != null){
baos.close();
}
}
}
问题解决!
getOutputStream() has already been called for this response的更多相关文章
- java.lang.IllegalStateException: getOutputStream() has already been called for this response
ERROR [Engine] StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exceptionjava.lang ...
- 用java实现文件下载,提示java.lang.IllegalStateException: getOutputStream() has already been called for this response
1. 用java实现文件下载,提示java.lang.IllegalStateException: getOutputStream() has already been called for this ...
- 报错记录:getOutputStream() has already been called for this response
仅作记录:参考文章:http://www.blogjava.net/vickzhu/archive/2008/11/03/238337.html 报错信息: java.lang.IllegalStat ...
- JSP文件下载及出现getOutputStream() has already been called for this response的解决方法
JSP文件下载及出现getOutputStream() has already been called for this response的解决方法 http://iamin.blogdriver.c ...
- 解决getOutputStream() has already been called for this response
http://qify.iteye.com/blog/747842 —————————————————————————————————————————————————— getOutputStream ...
- getOutputStream() has already been called for this response异常的原因和解决方法
今天在调试一个小web项目时,验证码不显示了,而且后台报错 getOutputStream() has already been called for this response 经过查找得知: 在t ...
- 在Struts2中使用poi进行excel操作下载的时候报getOutputStream() has already been called for this response 错误 [转]
在项目中用到了poi这个开源的操作excel文件的jar. 项目中用到struts2容器管理servlet.不是单纯的直接用servlet. workbook.write(os); ...
- 严重: Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException: getOutputStream() has already been called for this response
严重: Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException: getOutputS ...
- JSP:getOutputStream() has already been called for this response
JSP页面,用小脚本显示一张图片 <%@page import="java.io.OutputStream"%> <%@page import="jav ...
随机推荐
- javaScript系列:JSON详解
JSON详解 JSON的全称是”JavaScript Object Notation”,意思是JavaScript对象表示法,它是一种基于文本,独立于语言的轻量级数据交换格式.XML也是一种数据交 ...
- Java web开发,在一个jsp里放太多java代码的后果,摘自 java web轻量级开发面试教程
现要做一个简单的登录页面,如果用户通过验证,会显示Welcome用户名的欢迎词,反之则返回登录页面让用户再次输入 这部分的完整代码是JSPDemo项目里的login.jsp,下面来分析一下关键代码. ...
- 渗透常用手工SQL注入语句合集
1.判断有无注入点; and 1=1 and 1=2 2.猜表一般的表的名称无非是admin adminuser user pass password 等..and 0<>(select ...
- MySQL查询1
1.将下列语句复制到sqlyog的询问栏 /*!40101 SET NAMES utf8 */; create table `t_student` ( `id` double , `stuName` ...
- java Io流中FileInputStream和BufferedInputStream的速度比较
首先是对FileInputStream 加上 FileOutputStream 对文件拷贝的应用 我这里拷贝的是一个视频.当然,你们拷贝什么都可以,当文件越大时效果越明显 下面是对BufferedIn ...
- 延迟实例化 Lazy<T>
之前写的设计模式 单例模式中,推荐了使用Lazy<T>来达到线程安全和减少系统资源消耗的作用. 作用及优点: 创建某一个对象需要很大的消耗,而这个对象在运行过程中又不一定用到,为了避免每次 ...
- SVN 通过IIS设置反向代理访问
原因 一个字,穷,没办法,只有一台机器 要当测试服务器还要做源码管理. 解决办法 通过IIS配置反向代理访问SVN,给SVN访问的HTTPS绑定上域名,就可以正常访问了. 1.修改SVN配置 把SVN ...
- vue-router的两种模式的区别
众所周知,vue-router有两种模式,hash模式和history模式,这里来谈谈两者的区别. ### hash模式 hash模式背后的原理是`onhashchange`事件,可以在`window ...
- 将位图导入为ArcGIS面要素
本文根据笔者经验,介绍一种从位图图像导入ArcGIS称为要素的方法.这种方法适用于从现有出版物图片中获取地理信息的情况. 首先要说明的是,从位图导入要素是非常非常不精确的方式,如果有其它数据来源,那么 ...
- linux 增量备份命令Rsync 使用详解
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt320 Rsync的命令格式可以为以下六种: rsync [OPTION].. ...