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 分钟前
《C语言学习:编程例题》8
c语言·开发语言·c++·学习·算法·visual studio
Java面试题总结4 分钟前
Python 入门(四)- Openpyxl 操作 Excel 教程
开发语言·python·excel
devpotato8 分钟前
人工智能(九)- Spring AI MCP客户端开发
java·mcp
gCode Teacher 格码致知10 分钟前
Javascript提高:Math.round 详解-由Deepseek产生
开发语言·javascript
疯狂打码的少年16 分钟前
【Day14 Java转Python】从Java到Python——用Python重构一个Java小工具(文件批量重命名实战)
java·python·重构
广州灵眸科技有限公司17 分钟前
瑞芯微(EASY EAI)RV1126B QT GUI例程方案
linux·服务器·开发语言·网络·人工智能·qt·物联网
枫叶丹420 分钟前
【HarmonyOS 6.0】ArkWeb 私有网络访问控制接口详解
开发语言·网络·华为·harmonyos
小杍随笔20 分钟前
【Rust 1.95.0 正式发布!语言特性、标准库、平台支持全面升级,一文带你看完整更新】
开发语言·rust·策略模式
无籽西瓜a21 分钟前
【西瓜带你学设计模式 | 第十八期 - 命令模式】命令模式 —— 请求封装与撤销实现、优缺点与适用场景
java·后端·设计模式·软件工程·命令模式
aXin_ya24 分钟前
微服务 第二天
java·数据库·微服务