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;
    }
相关推荐
阿正的梦工坊8 小时前
JavaScript 微任务与宏任务完全指南
开发语言·javascript·ecmascript
chools8 小时前
【AI超级智能体】快速搞懂工具调用Tool Calling 和 MCP协议
java·人工智能·学习·ai
知行合一。。。9 小时前
Python--05--面向对象(属性,方法)
android·开发语言·python
李白你好9 小时前
TongWeb EJB 反序列化生成工具(Java-Chain 插件)
java·安全
青梅橘子皮9 小时前
C语言---指针的应用以及一些面试题
c语言·开发语言·算法
浅时光_c9 小时前
3 shell脚本编程
linux·开发语言·bash
Evand J9 小时前
【三维轨迹目标定位,CKF+RTS,MATLAB程序】基于CKF与RTS平滑的三维非线性目标跟踪(距离+方位角+俯仰角)
开发语言·matlab·目标跟踪
U盘失踪了10 小时前
Java 的 JAR 是什么?
java·jar
今天又在写代码10 小时前
java-v2
java·开发语言
competes11 小时前
慈善基金投资底层逻辑应用 顶层代码低代码配置平台开发结构方式数据存储模块
java·开发语言·数据库·windows·sql