getOutputStream() has already been called for this response异常处理
来源:互联网 发布:老挝嫖尸妓院图片知乎 编辑:程序博客网 时间:2024/06/05 15:14
转载地址:http://www.cnblogs.com/Orange42/p/6168803.html
错误日志里偶尔会有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(); } } }
问题解决!
阅读全文
1 0
- getOutputStream() has already been called for this response 异常处理
- getOutputStream() has already been called for this response异常处理
- 转异常getOutputStream() has already been called for this response
- getOutputStream() has already been called for this response异常
- 异常:getOutputStream() has already been called for this response
- 异常 getOutputStream() has already been called for this response
- JAVA异常 getOutputStream() has already been called for this response
- getOutputStream() has already been called for this response异常
- 抛出异常getOutputStream() has already been called for this response
- getOutputStream() has already been called for this response 错误异常的处理
- java_lang_IllegalStateException getOutputStream() has already been called for this response
- getOutputStream() has already been called for this response 的解决方法
- getOutputStream() has already been called for this response
- getOutputStream() has already been called for this response的解决方法
- getOutputStream() has already been called for this response
- getOutputStream() has already been called for this response
- getOutputStream() has already been called for this response
- getOutputStream() has already been called for this response 的解决方法
- 解读:HTTP协议
- InetAddress类概述与实例
- opencv安装包,在mingw,Codeblocks的配置
- UVA 253
- github常见操作和常见错误!错误提示:fatal: remote origin already exists. 【转载】
- getOutputStream() has already been called for this response异常处理
- fileinput上传文件
- UVA 10025
- Python基础入门学习
- ArrayList,LinkedList,Vestor
- 【grunt整合版】30分钟学会使用grunt打包前端代码
- Android odex优化提高首次开机速度
- android消息推送(一)——消息推送简介
- 类似密室逃脱的小游戏