错误:Attempted read from closed stream尝试读取关闭的流!!!
错误:Attempted read from closed stream尝试读取关闭的流!!!
原因:一个HttpClient里只能有一次获取Entity的操作,不能有多次获取Entity的操作,getEntity()获取的流是一次性的,用了一次就会自动关闭,第二次调用的时候就是调一个关闭的流,就会抛出:Attempted read from closed stream;
解决办法:如果一个方法只对同一个Entity操作,可以用BufferedHttpEntity,可以多次读取流!
HttpEntity entity = httpResponse.getEntity();if (!Objects.isNull(entity)) {try {//entity实体流保存缓冲区,否则只能操作一次流就会关闭 ,BufferedHttpEntity可以多次读取流entity = new BufferedHttpEntity(entity);responseBody = URLEncodedUtils.parse(entity).toString();} catch (IOException e) {e.printStackTrace();LogUtils.error(e, log, "io转换错误");}}
如果一个方法只对不同一个Entity操作,可以用一个临时的变量接收获取的Entity,再用临时变量走后续的逻辑!!!
这是没用临时变量接收之前,运行报错:Attempted read from closed stream
private static String getRemoteCallLogStr(HttpUriRequest httpUriRequest, HttpResponse httpResponse, long start, String method, Exception ex, String siteName) {String startTime = DateFormatUtils.format(new Date(start), "yyyy-MM-dd'T'HH:mm:ss.SSS");String path = "";String targetUrl = "";String uri = "";String targetHost = "";String requestheader = "";String responseHeader = "";String responseBody = "";Integer responseStatus = null;if (!Objects.isNull(httpUriRequest)) {if (!Objects.isNull(httpUriRequest.getURI())) {path = httpUriRequest.getURI().getPath();targetUrl = httpUriRequest.getURI().toString();targetHost = httpUriRequest.getURI().getHost();uri = httpUriRequest.getURI().toString();}requestheader = JSON.toJSONString(httpUriRequest.getAllHeaders());}if (!Objects.isNull(httpResponse)) {if (!Objects.isNull(httpResponse.getStatusLine())) {responseStatus = httpResponse.getStatusLine().getStatusCode();}HttpEntity entity = httpResponse.getEntity();if (!Objects.isNull(entity)) {try {responseBody = URLEncodedUtils.parse(entity).toString();System.out.println(responseBody);} catch (IOException e) {LogUtils.error(e, log, "io转换错误");}}responseHeader = JSON.toJSONString(httpResponse.getAllHeaders());}RemoteCallLog content = RemoteCallLog.builder().type("remote").loggerName(HttpsUtils.class.getName()).level(ex == null ? "INFO" : "ERROR").logId(LogUtils.getLogId()).time(startTime).hostName(EnvironmentUtil.getHostName()).targetMethod(path).targetUrl(targetUrl).requestId(LogUtils.initRequestIdIfAbsent()).uri(uri).requestMethod(method).status(ex == null ? "success" : "fail").duration(System.currentTimeMillis() - start).requestHeader(requestheader).responseBody(responseBody).responseHeader(responseHeader).stackTrace(ex == null ? "" : ExceptionUtils.getFullStackTrace(ex)).build();if ("POST".equals(method)) {content.setTargetSystem(siteName);} else {content.setTargetSystem(targetHost);}if (!Objects.isNull(responseStatus)) {content.setResponseStatus(responseStatus);}if ("GET".equals(method) && !Objects.isNull(httpUriRequest) && !Objects.isNull(httpUriRequest.getURI())) {content.setRequestUrlParam(httpUriRequest.getURI().getQuery());} else {HttpPost httpPost = (HttpPost) httpUriRequest;HttpEntity entity = httpPost.getEntity();try {content.setRequestBodyParam(URLEncodedUtils.parse(entity).toString());System.out.println(content.getRequestBodyParam());} catch (IOException e) {LogUtils.error(e, log, "io转换错误");e.printStackTrace();}}return JSONObject.toJSONString(content);}
加了临时变量之后,就没有抛:Attempted read from closed stream!
HttpEntity temp=null;
HttpEntity entity = httpResponse.getEntity();
temp=entity;
if (!Objects.isNull(entity)) {try {responseBody = URLEncodedUtils.parse(temp).toString();} catch (IOException e) {e.printStackTrace();LogUtils.error(e, log, "io转换错误");}
}
HttpEntity requestTemp=null;
HttpPost httpPost = (HttpPost) httpUriRequest;
HttpEntity entity = httpPost.getEntity();
requestTemp=entity;
try {content.setRequestBodyParam(URLEncodedUtils.parse(requestTemp).toString());
} catch (IOException e) {e.printStackTrace();LogUtils.error(e, log, "io转换错误");
}
错误:Attempted read from closed stream尝试读取关闭的流!!!相关推荐
- 使用HttpClient的时候报错java.io.IOException: Attempted read from closed stream
使用HttpClient的时候报错java.io.IOException: Attempted read from closed stream 问题背景 解决方案 心得 Lyric: 我们愉快的梦游 ...
- java后台调用SOE时,报异常java.io.IOException: Attempted read from closed stream
java后台调用SOE时,报异常java.io.IOException: Attempted read from closed stream 代码诸如此类 public void test(HttpS ...
- Attempted read from closed stream
今天遇到从Httpclient中获取Entity时报的错误 java.io.IOException: Attempted read from closed stream.at org.apache.h ...
- Attempted read from closed stream.
今天遇到这个问题,记录一下,从Httpclient中获取Entity时报的错误 java.io.IOException: Attempted read from closed stream.at or ...
- java.io.IOException: Attempted read from closed stream.
项目场景: 提示:读取pdf文件到本地: 读取文件流读取失败 问题描述 读取文件流读取失败 2022-06-08 09:00:52 [http-nio-8000-exec-4] ERROR m.z.e ...
- java.io.IOException: Attempted read from closed stream. 异常,解决
在HttpClient请求的时候,返回结果解析时出现java.io.IOException: Attempted read from closed stream. 异常,解决 原因是EntityUti ...
- 在HttpClient请求的时候,返回结果解析时出现java.io.IOException: Attempted read from closed stream. 异常,解决
在HttpClient请求的时候,返回结果解析时出现java.io.IOException: Attempted read from closed stream. 异常,解决 参考文章: (1)在Ht ...
- Attempted read from closed stream.问题
个人问题记录 0.0.4 问题描述:Attempted read from closed stream. 发现问题:通过httpclient获取页面返回信息报错 问题原因:httpclient的获取实 ...
- Java 报错Attempted read from closed stream
这是由于EntityUtils.toString(entity)方法只能调用一次,entity所得到的流是不可重复读取的也就是说所得的到实体只能一次消耗完,不能多次读取.
最新文章
- 存储过程—导出table数据为inser sqlt语句
- 2020\Simulation_2\2.最多边数
- XUtils之注解机制详解
- 不同php文件,php-不同文件夹的不同登录(会话)
- MySQL 数据库怎样把一个表的数据插入到另一个表
- 洛谷 P1966 火柴排队 —— 思路
- antdesignpro 重定向到登录页面_URL重定向跳转绕过
- 机器学习之线性回归的改进-岭回归
- Java 常用的依赖包
- 2021-06-01winserver2008R2溢出提权(转)
- 封装自己的js库(二)---仿照JQuery
- 二叉树的基本性质及证明
- 向Hexo博客添加微博秀(pug模板)
- 罗克韦尔自动化收购工业自动化系统模拟与仿真的领先软件开发商Emulate3D
- Java awaitUninterruptibly用法
- FMT(基于rtthread)开源代码编译 win10下编译环境搭建及编译
- 声网如何处理直播 SEI 相关问题?
- 记录一次云服务器被劫持下载了挖矿病毒的处理过程
- 仓库管理系统————QT+SQLite实现
- 全兼容增强版zc706单板照片
热门文章
- echarts 实现柱状图左右横向对比显示
- 餐厅小票打印模板_智慧餐厅多商家入驻小程序开发,成功案例:万足外卖!
- 论文阅读:(arXiv 2021)Beyond Periodicity: Towards a Unifying Framework for Activations in Coordinate-MLP
- 如何使用启动盘PE桌面工具安装原版win7系统?
- 关于医疗影像的mhd和dcm格式图像的读取和坐标转换
- ESP32基于arduino开发的心跳体温检测系统(四)本地端系统整合
- php过滤微信表情符号的正则表达式方法
- Java版本和JDK版本对应关系
- 使用selenium爬取唯品会
- 【前端】html如何添加icon