拷贝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
        }
    }
『愿你我既可以朝九晚五,又能够浪迹天涯』
原文地址:https://www.cnblogs.com/zjwwljty/p/7199582.html