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;
    }
相关推荐
NE_STOP7 小时前
Vide Coding--AI编程工具的选择
java
LDR0067 小时前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术7 小时前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园7 小时前
C++20 Modules 模块详解
java·开发语言·spring
程序员黑豆7 小时前
JDK 下载安装与配置详细教程
java·前端·ai编程
小宇宙Zz8 小时前
Maven依赖冲突
java·服务器·maven
swordbob8 小时前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
咖啡八杯8 小时前
GoF设计模式——享元模式
java·spring·设计模式·享元模式
十五喵源码网8 小时前
基于springboot2+vue2的租房管理系统
java·毕业设计·springboot·论文笔记
摇滚侠8 小时前
IDEA 创建 Java 项目 手动整合 SSM 框架
java·ide·intellij-idea