开发者社区> 问答> 正文

HttpURLConnection 问题

为什么 String key = DigestUtils.md5Hex(conn.getInputStream()); 使用后,后面就不能用了,难道必须要两次 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 吗?

 @Test
    public void test() {
        try {
            URL url = new URL("http://www.baidu.com/img/270_fbe71c0815a61fd81c5b89573a002f38.png");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");// 声明请求方式
            conn.setConnectTimeout(6 * 1000);// 设置连接超时
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon;)");
            conn.setRequestProperty("Accept-Encoding", "identity");
            
            if (conn.getResponseCode() == 200) {
                String key = DigestUtils.md5Hex(conn.getInputStream());
                System.out.println(key);
                
                BufferedImage img = ImageIO.read(conn.getInputStream());
                int width = img.getWidth();
                int height = img.getHeight();
                System.out.println(width);
                System.out.println(height);
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

展开
收起
蛮大人123 2016-03-09 18:21:35 2090 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    输入流在读取的时候,读取之后的数据就不会再次被读取(有些流可以通过设置Position的位置重复读取,这里不考虑),所以你需要将输入流中的内容读取到一个byte[]缓存起来,以重复使用.

    2019-07-17 18:56:30
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载