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;
    }
相关推荐
xlq223221 小时前
22.多态(上)
开发语言·c++·算法
666HZ6661 小时前
C语言——高精度加法
c语言·开发语言·算法
代码or搬砖1 小时前
MyBatisPlus讲解(二)
java·mybatis
星释1 小时前
Rust 练习册 100:音乐音阶生成器
开发语言·后端·rust
lcu1111 小时前
Java 学习42:抽象
java
Mr.朱鹏1 小时前
RocketMQ安装与部署指南
java·数据库·spring·oracle·maven·rocketmq·seata
雨中飘荡的记忆1 小时前
Spring表达式详解:SpEL从入门到实战
java·spring
Coder-coco1 小时前
个人健康管理|基于springboot+vue+个人健康管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·mysql·论文
5***26222 小时前
Spring Boot问题总结
java·spring boot·后端
风生u2 小时前
go进阶语法
开发语言·后端·golang