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 ...
随机推荐
- 支持Linux系统的加密狗
深思数盾 https://www.sense.com.cn/ 产品:精锐5 版本:标准版.精灵版.IE版.时钟锁 快速实现高安全度的软件保护,轻松定义多种授权模式1.防止软件盗版,防止逆向工程 通过增 ...
- Scala中foldLeft的总结
源码分析 def seq: TraversableOnce[A] 上面两段代码是scala.collection.TraversableOnce特质的foldLeft方法源代码,实现了Traversa ...
- python类与对象-如何派生内置不可变类型并修其改实例化行为
如何派生内置不可变类型并修其改实例化行为 问题举例 自定义一种新类型的元组,对传入的可迭代对象,我们只保留 其中int类型且值大于0的元素,例如 IntTuple([1, -1, 'abc', 6, ...
- layer倒计时弹框/弹层 DEMO
layer.msg("提示语...", { time: 5000, shade: 0.6, success: function (layero, index) { var msg ...
- Mac对gdb签名
codesign -f -s gdb-cert $(which gdb) gdb_cert为自己创建的证书名 添加证书信任 activity-monitor双击taskgated 关闭 gdb成功运行
- Shadow DOM及自定义标签
参考链接:点我 一.什么是Shadow DOM Shadow DOM,直接翻译的话就是 影子 DOM,可以理解为潜藏在 DOM 结构中并且我们无法直接控制操纵的 DOM 结构.类似于下面这种结构 Sh ...
- 003-单例OR工厂模式
单例模式:DbContextFactory.cs using CZBK.ItcastOA.Model; using System; using System.Collections.Generic; ...
- sitecore8.2 基于相对路径查询item
当前项目: bar (path: /sitecore/content/home/foo/bar) 查询: query:./child/grandchild 结果: grandchild (path: ...
- wm_concat函数的排序问题
wm_concat在行转列的时候非常有用,但在行转列的过程中的排序问题常常难以控制. 可见下面例子: 准备测试表: drop table t; create table t (n number,m n ...
- 关于 mysql2 -v '0.3.21'(CentOS7.3)
个人由于没有安装mysql而是装的MariaDB,所以网上说安装mysql,故没有采用,经查阅资料后,详细情况如下: Gem时报错: [root@localhost ~]# gem install m ...