Java 远程url文件sha256加密

Java 远程url文件sha256加密

java 复制代码
public static String getSHA256(String filePath) throws Exception {
        InputStream fis = null;
        URL url = new URL(filePath);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] buffer = new byte[1024];

        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            fis = connection.getInputStream();
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) {
                digest.update(buffer, 0, bytesRead);
            }
            fis.close();
        } else {
            throw new IOException("HTTP request failed with response code: " + responseCode);
        }

        byte[] sha256Bytes = digest.digest();

        StringBuilder sb = new StringBuilder();
        for (byte b : sha256Bytes) {
            sb.append(String.format("%02x", b));
        }

        String sha256 = sb.toString();
        return sha256;
    }
相关推荐
Beyond_System|系统之外18 分钟前
【学编程】Python基础编程题100道(21-60)
开发语言·python·算法
景熙552340 分钟前
15.Java 8 Stream 流入门到实战
java·开发语言·数据结构
wno7041 小时前
Spring Security权限控制
java·python·spring
杨运交2 小时前
[071][验证码模块]基于Spring拦截器的验证码认证设计思想
java·后端·spring
Geek-Chow2 小时前
CountDownLatch in Java
java
SL_staff3 小时前
从RBAC到场景化授权:《无忧·企业文档》三级权限模型的技术实践解析
java·开源·产品
传奇开心果编程3 小时前
【springboot基础语法学与练】第 1 课:从零开始
java·spring boot·后端·学习
SL_staff3 小时前
财务系统慎用低代码?从数据模型闭环看合规落地的技术实践
java·低代码·全栈
Bruce_Liuxiaowei3 小时前
Python 实例赋值遮蔽类属性:一个从不报错的静默陷阱
开发语言·python·语法糖
滕州市燕猫虎计算机科技工作室个体工商户3 小时前
IDEA:Command line is too long
java·ide·intellij-idea