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;
    }
相关推荐
行思理1 分钟前
FastAdmin新手教程
java·开发语言·fastadmin
就叫飞六吧1 分钟前
py脚本一键生成常见文件格式案例
开发语言·python
向上的车轮4 分钟前
Apache Camel 与 Spring Integration的区别是什么?
java·spring·apache
Tony Bai8 分钟前
Go 性能分析的“新范式”:用关键路径分析破解高并发延迟谜题
开发语言·后端·golang
lly20240612 分钟前
MySQL 创建数据库
开发语言
nsjqj13 分钟前
JavaEE初阶:计算机是如何工作的
java·java-ee
URBBRGROUN46714 分钟前
Spring AI Alibaba入门
java·人工智能·spring
minglie117 分钟前
Vitis HLS c转verilog
c语言·开发语言·fpga开发
她和夏天一样热21 分钟前
【实战篇】设计模式在开发中的真实应用
java·开发语言·设计模式