根据网络url获取文件md5值 java

private String md5ByUrl(String imgUrl){
        String md5 = "";
        InputStream inputStream = null;
        try {
            //url路径
            URL url=new URL(imgUrl);
            //获取连接
            HttpURLConnection connection=(HttpURLConnection)url.openConnection();
            connection.setConnectTimeout(3*1000);
            //设置请求头
            connection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36");
            inputStream = connection.getInputStream();
            //DigestUtils为org.apache.commons.codec.digest.DigestUtils下的类
            md5 = DigestUtils.md5Hex(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                if (inputStream != null) {
                    //关闭流
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return  md5;
    }

 

转载自 

https://blog.csdn.net/zhengsaisai/article/details/108319918

上一篇:Mybatis【2.1】-- 从读取流到创建SqlSession发生了什么?


下一篇:InputStream.read([byte[]) 详解