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+AI 加强14-2 网络编程-UDP和TCP通信-线程和线程池优化通信-BS架构原理
java
金銀銅鐵14 分钟前
[Java] 浅析 Map.of(...) 方法和 Map.ofEntries(...) 方法
java·后端
邪恶紫色秋裤20 分钟前
解决IntelliJ IDEA控制台输出中文乱码问题
java·ide·乱码·intellij-idea·报错·中文
摇滚侠26 分钟前
Spring Boot3零基础教程,StreamAPI 的基本用法,笔记99
java·spring boot·笔记
代码匠心27 分钟前
从零开始学Flink:事件驱动
java·大数据·flink·大数据处理
yours_Gabriel31 分钟前
【分布式事务】Seata分布式解决方案
java·分布式·微服务
程序猿202337 分钟前
Python每日一练---第三天:删除有序数组中的重复项
开发语言·python
一只游鱼40 分钟前
Springboot+BannerBanner(启动横幅)
java·开发语言·数据库
一只游鱼42 分钟前
抖音上的用python实现激励弹窗
开发语言·python
codingPower44 分钟前
升级mybatis-plus导致项目启动报错: net.sf.jsqlparser.statement.select.SelectBody
java·spring boot·maven·mybatis