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;
    }
相关推荐
云深麋鹿3 分钟前
数据链路层总结
java·网络
fire-flyer4 分钟前
响应式客户端 WebClient详解
java·spring·reactor
Pocker_Spades_A19 分钟前
Python快速入门专业版(二十六):Python函数基础:定义、调用与返回值(Hello函数案例)
开发语言·python
北执南念21 分钟前
基于 Spring 的策略模式框架,用于根据不同的类的标识获取对应的处理器实例
java·spring·策略模式
王道长服务器 | 亚马逊云25 分钟前
一个迁移案例:从传统 IDC 到 AWS 的真实对比
java·spring boot·git·云计算·github·dubbo·aws
island131427 分钟前
【C++框架#5】Elasticsearch 安装和使用
开发语言·c++·elasticsearch
华仔啊29 分钟前
为什么 keySet() 是 HashMap 遍历的雷区?90% 的人踩过
java·后端
9号达人42 分钟前
Java 13 新特性详解与实践
java·后端·面试
橙序员小站1 小时前
搞定系统设计题:如何设计一个支付系统?
java·后端·面试
周周记笔记1 小时前
学习笔记:Python的起源
开发语言·python