使用HttpClient的时候报错java.io.IOException: Attempted read from closed stream
问题背景
使用HttpClient的时候报错java.io.IOException: Attempted read from closed stream
2022-01-21 14:48:30.627 ERROR 32956 --- [ main] c.dz.marketservice.util.HttpClientPool : =============["异常"]======================, e: {}
java.io.IOException: Attempted read from closed stream.
at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:165) ~[httpcore-4.4.15.jar:4.4.15]
at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:135) ~[httpclient-4.5.13.jar:4.5.13]
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284) ~[na:1.8.0_221]
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326) ~[na:1.8.0_221]
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178) ~[na:1.8.0_221]
at java.io.InputStreamReader.read(InputStreamReader.java:184) ~[na:1.8.0_221]
at java.io.Reader.read(Reader.java:140) ~[na:1.8.0_221]
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:227) ~[httpcore-4.4.15.jar:4.4.15]
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:308) ~[httpcore-4.4.15.jar:4.4.15]
at com.dz.marketservice.util.HttpClientPool.postForJsonObject(HttpClientPool.java:127) ~[classes/:na]
at com.dz.marketservice.client.HttpTest.packageReq(HttpTest.java:63) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_221]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_221]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_221]
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725) [junit-platform-commons-1.8.2.jar:1.8.2]
java.lang.NullPointerException
at com.dz.marketservice.client.HttpTest.packageReq(HttpTest.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
解决方案
1 看错误Attempted read from closed stream翻译过来就是:从已经关闭的流中尝试再次读取,根据这条报错,我去看了我的代码
System.out.println(EntityUtils.toString(response.getEntity()));
JSONObject result = JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
2 为了方便我自己加入了一条打印,重复使用了response.getEntity(),但是这个获取只能获取一次,就像发布与消费,当中间的消息消费掉了,再去使用,找不到就报错了,所以这条语句可以改成:
String temp = EntityUtils.toString(response.getEntity());
System.out.println(temp);
JSONObject result = JSONObject.parseObject(temp));
心得
- 耐心读一下报错信息,好好理解一下,也能自己解决问题
作为程序员第 30 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …
Lyric: 我们愉快的梦游
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/110846.html