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;
    }
相关推荐
Allen_LVyingbo几秒前
医疗AI新范式:当数理模型开始“计算”生命,传统大模型面临重构(中)
开发语言·人工智能·python·自然语言处理·重构·知识图谱
计算机毕设指导63 分钟前
基于微信小程序的校园二手交易系统【源码文末联系】
java·spring boot·spring·微信小程序·小程序·tomcat·maven
BD_Marathon3 分钟前
设计模式——迪米特法则
java·设计模式·迪米特法则
为什么不问问神奇的海螺呢丶3 分钟前
n9e categraf k8s监控配置 -kube-state-metrics
java·容器·kubernetes
时艰.4 分钟前
Java 线程池 — ThreadPoolExecutor
java·开发语言·python
Coder_Boy_5 分钟前
企业级项目高并发监控场景-Spring Boot 集成 Graphite & InfluxDB 实战文档
java·spring boot·后端·系统架构
暖馒6 分钟前
深度剖析串口通讯(232/485)
开发语言·c#·wpf·智能硬件
lang201509289 分钟前
Java EE并发工具:JSR 236详解
java·java-ee
新新学长搞科研9 分钟前
【CCF主办 | 高认可度会议】第六届人工智能、大数据与算法国际学术会议(CAIBDA 2026)
大数据·开发语言·网络·人工智能·算法·r语言·中国计算机学会
梵刹古音2 小时前
【C语言】 字符数组相关库函数
c语言·开发语言·算法