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;
    }
相关推荐
java 乐山8 分钟前
c 写一个文本浏览器(1)
c语言·开发语言
windows_622 分钟前
MISRA C:2025 规则逐条分析
c语言·开发语言
fie88891 小时前
基于MATLAB的可见光通信(VLC)系统仿真
开发语言·matlab
写代码的【黑咖啡】1 小时前
Python中的lxml:高效XML处理库
xml·开发语言·python
黎雁·泠崖1 小时前
Java字符串高阶:底层原理深剖+经典面试题全解
java·开发语言
清风~徐~来1 小时前
【视频点播系统】环境搭建
开发语言
重生之我是Java开发战士1 小时前
【Java SE】反射、枚举与Lambda表达式
java·开发语言
weixin_436525071 小时前
若依多租户版 - @ApiEncrypt, api接口加密
java·开发语言
superman超哥1 小时前
序列化格式的灵活切换:Serde 生态的统一抽象力量
开发语言·rust·编程语言·rust serde·序列化格式·rust序列化格式
Hello.Reader1 小时前
Flink Java 版本兼容性与 JDK 模块化(Jigsaw)踩坑11 / 17 / 21 怎么选、怎么配、怎么稳
java·大数据·flink