java.io.IOException: Attempted read from closed stream
前言:
代码如下,执行的时候提示“java.io.IOException: Attempted read from closed stream.”
public static JSONObject post(String url,StringBuffer params,String token ){
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url + "?" + params);
httpPost.setHeader("Content-Type", "application/json, text/plain, */*");
httpPost.setHeader("Authorization",token);
// 响应模型
CloseableHttpResponse response = null;
try {
// send the Post Request
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
System.out.println("响应内容长度为:" + responseEntity.getContentLength());
System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
String jsonString = EntityUtils.toString(responseEntity);
resBody = JSONObject.fromObject(jsonString);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//release resource
release(httpClient,response);
}
return resBody;
}
原因
response.getEntity()所得到的流是不可重复读取的,所得的实体只能读取一次,读取一次后,流就关闭了。EntityUtils.toString(responseEntity)被调用一次后就会自动销毁,而我调用了2次,所以就报错了。
System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
String jsonString = EntityUtils.toString(responseEntity);
解决方法
把这2个输出脚本改为如下即可,只要调用一次就好:
String jsonString = EntityUtils.toString(responseEntity);
System.out.println("响应内容为:" + jsonString);
java.io.IOException: Attempted read from closed stream的更多相关文章
- 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. 原始代码: public static S ...
- 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. 异常,解决
原因是EntityUtils.toString(HttpEntity)方法被使用了多次.所以每个方法内只能使用一次.
- java.io.IOException: Stream closed
今天在做SSH项目的时候,出现了这个错误.百思不得其解,网上的答案都不能解决我的问题-.. 后来,一气之下就重新写,写了之后发现在JSP遍历集合的时候出错了. <s:iterator value ...
- java.io.IOException: Stream closed解决办法
1.出现这个bug的大体逻辑代码如下: private static void findMovieId() throws Exception { File resultFile = new File( ...
- java.io.IOException: Messenger was closed
程序运行一段时间后抛出异常java.io.IOException: Messenger was closed,不知道是啥原因? ———————————————————————————————————— ...
- Caused by: java.io.IOException: Filesystem closed的处理
org.apache.hadoop.hive.ql.metadata.HiveException: Unable to rename output from: hdfs://nameservice/u ...
- java.io.IOException: read failed, socket might closed or timeout, read ret: -1
近期项目中连接蓝牙之后接收蓝牙设备发出的指令功能,在连接设备之后,创建RfcommSocket连接时候报java.io.IOException: read failed, socket might c ...
随机推荐
- maven插件--assembly
之前maven项目中使用assembly插件单独打包项目依赖包,项目只有一个模块也就一个pom,配置这个插件,一切很顺利.但是现在的项目复杂了,有parent有child,多模块.按照之前的做法怎么也 ...
- allegro17.2 gerber 步骤
1.Manufacture -> NC -> Drill Customization... 先点击 Auto generate symbols,出来对话框后点击 YES .然后在Symbo ...
- [vue]vue基础复习项案例stepbystep
看本篇第二次复习内容即可. 还有一些 文档了这个如 https://www.cnblogs.com/iiiiiher/p/9508733.html https://www.cnblogs.com/ii ...
- Centos7 HyperLedger Fabric 1.4 生产环境部署
Kafka生产环境部署案例采用三个排序(orderer)服务.四个kafka.三个zookeeper和四个节点(peer)组成,共准备八台服务器,每台服务器对应的服务如下所示: kafka案例网络拓扑 ...
- Maven -- 在进行war打包时排除不需要的文件
https://blog.csdn.net/zsg88/article/details/78128603 <excludes> <!-- 排除文件,不包含子目录,对WEB-INF目录 ...
- python项目推荐(转载知乎)
作者:Wayne Shi链接:https://www.zhihu.com/question/29372574/answer/88744491来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商 ...
- Postman接口自动化测试实例用到的完整的SM2前端加密算法代码
var __g__ = {}; !function(t,e){"object"==typeof exports?module.exports=exports=e():"f ...
- tensorflow(4)踩过的一些坑
版本问题 1.1 版本的一个BUG ValueError: Variable rnn/basic_lstm_cell/weights already exists, disallowed. 结合这个文 ...
- jsp与jsp页面间的值传递与接收
1.使用<a>标签 传递值 <a href="index.jsp?name=增加数据">增加数据</a> ///////目标页面/////值// ...
- mongodb和python交互
一.安装pymongo包 sudo pip install pymongo 二.新增数据: 增加一条: from pymongo import MongoClient client = MongoCl ...