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 分钟前
AI 智能体安全踩坑记:Java 为 OpenClaw 添加权限控制与审计日志实战
java·人工智能
晓晓hh13 分钟前
JavaSe学习——基础
java·开发语言·学习
清水白石00825 分钟前
Python 内存陷阱深度解析——浅拷贝、深拷贝与对象复制的正确姿势
开发语言·python
phltxy32 分钟前
算法刷题|模拟思想高频题全解(Java版)
java·开发语言·算法
wuyikeer32 分钟前
Java进阶(ElasticSearch的安装与使用)
java·elasticsearch·jenkins
C雨后彩虹37 分钟前
深入探索Java Stream:6个复杂业务场景下的高效实现方案
java·多线程·stream·同步·异步
愚者游世39 分钟前
template学习大纲
开发语言·c++·程序人生·面试·visual studio
阿里嘎多学长41 分钟前
2026-03-11 GitHub 热点项目精选
开发语言·程序员·github·代码托管
宵时待雨42 分钟前
C++笔记归纳10:继承
开发语言·数据结构·c++·笔记·算法
csbysj202043 分钟前
TypeScript String
开发语言