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 ...
随机推荐
- 【托业】【全真题库】TEST1-语法题
TEST01 103. delivery date 交货日期 delivery n.传送,投递; [法](正式)交付; 分娩; 讲演; 104. net revenue 净收入,纯收入 105. re ...
- ADB——应用交互
使用ADB与手机应用交互 应用交互的操作包括:启动应用 / 调起Activity.调起Services.停止Service.发送广播.强行停止应用 基本命令 am <command> 常用 ...
- HTML个人简介
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...
- 内网gitlab11.2升级至11.4.5
当前gitlab版本 宿主机是一台ubuntu 运行备份命令 sudo gitlab-rake gitlab:backup:create STRATEGY=copy 升级命令 sudo apt-get ...
- hadoop集群的规划和搭建
1.操作系统版本:CentOS 6 CM版本:CM5.x CDH版本:CDH5.x 2.安装操作系统,对系统盘做 RAID1: 配置静态IP.hostname信息:vim /etc/sysconfig ...
- Grunt搭建自动化web前端开发环境--完整流程
Grunt搭建自动化web前端开发环境-完整流程 jQuery在使用grunt,bootstrap在使用grunt,百度UEditor在使用grunt,你没有理由不学.不用! 1. 前言 各位web前 ...
- Respone弹窗
Response.Write("<script>window.open('default.aspx?iID=" + GridView1.DataKeys[GridVie ...
- goldsun取经----python2与 python3的差异
python2与 python3的差异 1.编码方式 python2中有ASCII str()类型,unicode是单独的,不是byte类型,不支持中文 python3中有Unicode(utf-8) ...
- java-redis
pom.xml添加如下配置: <dependency> <groupId>org.springframework.boot</groupId> <artifa ...
- [批处理] Git中log的使用
1.获取两个提交之间的日志: git log SHA-1_A.. SHA-1_B--pretty=format:"%cd: %s" --date=format:%Y%m%d > ...