拷贝InputStream

    public void testIOCopy (CloseableHttpResponse response){
        //流只能读取一遍,以下方法可以实现流的复用
        try {
            HttpEntity entity = response.getEntity();
            InputStream in = entity.getContent();
            ByteArrayOutputStream out = new ByteArrayOutputStream();  
            byte[] buffer = new byte[1024];  
            int len;  
            while ((len = in.read(buffer)) > -1 ) {  
                out.write(buffer, 0, len);   
            }  
            out.flush(); 
            out.close();
            in.close();
            response.close();
            //获取照片流
            byte[] byteArray = out.toByteArray();
            InputStream inputStream1 = new ByteArrayInputStream(byteArray); 
            InputStream inputStream2 = new ByteArrayInputStream(byteArray);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

 

posted @ 2017-07-18 11:30  不像程序员  阅读(1953)  评论(0编辑  收藏  举报