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;
    }
相关推荐
froginwe111 分钟前
Bootstrap4 输入框组
开发语言
她说..1 分钟前
Spring AOP场景5——异常处理(附带源码)
java·数据库·后端·spring·springboot·spring aop
listhi5202 分钟前
matlab大规模L1范数优化问题
开发语言·matlab
傅里叶的耶6 分钟前
C++ Primer Plus(第6版):第二章 开始学习C++
开发语言·c++·学习
醇氧7 分钟前
springAI学习 (二) 模型
java·学习·spring·ai·ai编程
雾岛听蓝7 分钟前
C++ 类和对象(二):默认成员函数详解
开发语言·c++
爱吃大芒果7 分钟前
Flutter 动画实战:隐式动画、显式动画与自定义动画控制器
开发语言·javascript·flutter·ecmascript·gitcode
风月歌9 分钟前
小程序项目之驾校报名小程序源代码(java+vue+小程序+mysql)
java·vue.js·mysql·小程序·毕业设计·源码
郝学胜-神的一滴10 分钟前
OpenGL中的glDrawArrays函数详解:从基础到实践
开发语言·c++·程序人生·算法·游戏程序·图形渲染
毕设源码-赖学姐11 分钟前
【开题答辩全过程】以 高校课程建设管理系统为例,包含答辩的问题和答案
java·eclipse