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;
    }
相关推荐
dllxhcjla7 小时前
微服务全套
java
亚历克斯神7 小时前
JVM 内存管理 2026:深度解析与调优实战
java·spring·微服务
以神为界7 小时前
Python入门实操:基础语法+爬虫入门+模块使用全指南
开发语言·网络·爬虫·python·安全·web
逻辑驱动的ken8 小时前
Java高频面试题:03
java·开发语言·面试·求职招聘·春招
噜噜大王_9 小时前
深入理解 C 语言内存操作函数:memcpy、memmove、memset、memcmp
c语言·开发语言
广师大-Wzx9 小时前
一篇文章看懂MySQL数据库(下)
java·开发语言·数据结构·数据库·windows·python·mysql
野生技术架构师9 小时前
Java NIO到底是个什么东西?
java·开发语言·nio
lolo大魔王9 小时前
Go语言的异常处理
开发语言·后端·golang
likerhood10 小时前
简单工厂设计模式
java·ide·intellij-idea
chh56310 小时前
C++--模版初阶
c语言·开发语言·c++·学习·算法