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;
    }
相关推荐
萧曵 丶1 分钟前
Java 安全的单例模式详解
java·开发语言·单例模式
Qiuner4 分钟前
Spring Boot 全局异常处理策略设计(一):异常不只是 try-catch
java·spring boot·后端
superman超哥6 分钟前
Rust 错误处理模式:Result、?运算符与 anyhow 的最佳实践
开发语言·后端·rust·运算符·anyhow·rust 错误处理
lly2024067 分钟前
Web 品质样式表
开发语言
Wang's Blog7 分钟前
Nodejs-HardCore: 模块管理与I/O操作详解
开发语言·nodejs
强子感冒了7 分钟前
Java List学习笔记:ArrayList与LinkedList的实现源码分析
java·笔记·学习
微爱帮监所写信寄信8 分钟前
微爱帮监狱寄信写信小程序PHP底层优化框架
java·开发语言·数据库·spring·微信·php·mybatis
琥珀.8 分钟前
查看linux下java服务进程是否正常
java·linux·运维
lly20240610 分钟前
R 语言注释指南
开发语言
Coder_Boy_15 分钟前
基于SpringAI企业级智能教学考试平台核心模块协同总结与最佳实践方案
java·大数据·人工智能