java.io.IOException: Attempted read from closed stream

代码如下,执行的时候提示“java.io.IOException: Attempted read from closed stream.”

 @Test
    public void test_temp(){
        String url="http://ssov1.59iedu.com/login?TARGET=http://med.ihbedu.com:80/gateway/web/sso/auth&js&callback=loginThen&1470491151264&nocache=1470491171451";
        this.HttpGet(url);

    }
    public void HttpGet(String url) {
        CloseableHttpClient httpClient = HttpClients.createDefault();//建立httpclient
        HttpGet httpGet = new HttpGet(url);//建立httpget
        System.out.println("get请求的地址:" + httpGet.getURI());
        try {
            CloseableHttpResponse response = httpClient.execute(httpGet);//执行get请求,并结果保存
            System.out.println("get请求返回的状态码:" + response.getStatusLine().getStatusCode());
            HttpEntity httpEntity = response.getEntity();//将保存的response转为实体
            try {

                if (httpEntity != null) {
                    {
                        System.out.println("get请求返回的response值:" + EntityUtils.toString(httpEntity));
                        System.out.println("lt的值:"+EntityUtils.toString(httpEntity).split("lt:\"")[1].split("\",")[0]);
                    }
                }
            } finally {
                EntityUtils.consume(httpEntity);//关闭实体
                response.close();//关闭response
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();//关闭httpclient
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

原因是如下特别指出我的脚本中以下2个输出,这个输出中调用了2次 EntityUtils.toString(httpEntity) ,而根据httpclient的官方说明中,EntityUtils.toString(httpEntity) 这个被调用一次后就会自动销毁,而我调用了2次所有就报错了

System.out.println("get请求返回的response值:" + EntityUtils.toString(httpEntity));
System.out.println("lt的值:"+EntityUtils.toString(httpEntity).split("lt:\"")[1].split("\",")[0]);

于是把这2个输出脚本改为如下即可,只要调用一次就好

String responseStr=EntityUtils.toString(httpEntity);
System.out.println("get请求返回的response值:" + responseStr);
String str=responseStr.split("lt:\"")[1].split("\",")[0];
System.out.println("lt的值:"+str);

时间: 2024-08-05 06:45:35

java.io.IOException: Attempted read from closed stream的相关文章

java.io.IOException: Stream closed 异常的原因和处理

java.io.IOException: Stream closed 多个线程索引同一个input  stream,当某一个thread在执行完之后,把这个inputstream关闭了:而此时正在从这个input  stream流中读取信息的线程就会抛出  java.io.IOException:  Stream  closed  异常. 终于找到这个异常的根源所在,原来是两个页面同时调用一个jsp,这个jsp中的内建对象out在执行out.close()时发生的异常,也就是当某一个thread

Java导出Excel,java.io.IOException: Stream is already closed

使用POI进行Excel导出的时候,后台报了这样一个错误: java.io.IOException: Stream is already closed 导出的代码如下: 查了半天,才发现问题出在第409行 out.close(); out这个输出流是不用自己手动关闭的,系统会自动替我们关闭,自己手动关闭的话还会引发问题. 实际上只有像下面这种使用new关键字创建的输入/输出流,才需要自己手动关闭 InputStream input = new FileInputStream(new File("

java.io.IOException: The stream is closed

at org.apache.commons.compress.archivers.zip.ZipArchiveInputStream.read(ZipArchiveInputStream.java:362) at java.io.InputStream.read(Unknown Source) at org.apache.commons.compress.utils.IOUtils.copy(IOUtils.java:75) at org.apache.commons.compress.util

java.io.IOException: read failed, socket might closed or timeout, read ret: -1

近期项目中连接蓝牙之后接收蓝牙设备发出的指令功能,在连接设备之后,创建RfcommSocket连接时候报java.io.IOException: read failed, socket might closed or timeout, read ret: -1错误.以下说一下我的解决方法,希望对各位有一点帮助. private BluetoothSocket mSocket; <span style="white-space:pre"> </span>priva

java.io.IOException: java.io.EOFException: Unexpected end of input stream错误

报错现象: Diagnostic Messages for this Task:Error: java.io.IOException: java.io.EOFException: Unexpected end of input stream at org.apache.hadoop.hive.io.HiveIOExceptionHandlerChain.handleRecordReaderNextException(HiveIOExceptionHandlerChain.java:121) at

坑爹微信之读取PKCS12流时出现的java.io.IOException: DerInputStream.getLength

背景 微信退款接口需要使用到证书,我参考微信的官方Demo进行,部分代码如下: char[] password = config.getMchID().toCharArray(); InputStream certStream = config.getCertStream(); KeyStore ks = KeyStore.getInstance("PKCS12"); ks.load(certStream, password); 上面的代码,在本地调试的时候正常跑过,没有出现任何异常,

Hive报错 Failed with exception java.io.IOException:java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: ${system:user.name%7D

报错信息如下 Failed with exception java.io.IOException:java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: ${system:user.name%7D 解决方法: 编辑 hive-site.xml 文件,添加下边的属性 <property> <name>system:java.io.tmpdir<

java.io.IOException: No space left on device 错误

今天碰到比较奇怪的问题: 7/05/14 19:20:24 INFO util.Utils: Fetching http://192.168.31.160:33039/jars/spark_study_java-0.0.1-SNAPSHOT-jar-with-dependencies.jar to /tmp/spark-446068a4-aaa4-4277-b009-908bf0d4ecac/executor-dcc3175b-7d19-4485-81e1-bf31a83a66b4/spark-

spark程序异常:Exception in thread &quot;main&quot; java.io.IOException: No FileSystem for scheme: hdfs

命令: java -jar myspark-1.0-SNAPSHOT.jar myspark-1.0-SNAPSHOT.jar hdfs://single:9000/input/word.txt hdfs://single:9000/output/out1 错误信息: .......... 14/11/23 06:14:18 INFO SparkDeploySchedulerBackend: Granted executor ID app-20141123061418-0011/0 on hos