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;
    }
相关推荐
Herbert_hwt14 分钟前
建立Java程序开发
java·开发语言
好好沉淀30 分钟前
@ExcelIgnoreUnannotated 和 @AutoMapper 详解
java
愚公移码1 小时前
蓝凌EKP18产品:流程虚拟机(PVM)
java·开发语言·前端
十月的皮皮1 小时前
stm20260720-从新手 C 到量产 STM32 工程:程序设计推导指南
c语言·开发语言·stm32·stm32cubemx·hal库
驰骋工作流1 小时前
流程引擎BPM设计之:流程消息
java·工作流引擎·bpm·jflow·ccflow
dogstarhuang1 小时前
从 0 到 1 搭建可收费的 API 开放平台(实战)
java·架构·api
VortMall2 小时前
『平台去经营化』平台治理能力全新重构|VortMall微服务商城系统v1.3.10
java·大数据·微服务·商城系统·开源商城·vortmall·去经营化
Echo缘2 小时前
嵌入式系统C语言资源分类与内存分布分析
c语言·开发语言
吴声子夜歌2 小时前
Redis 3.x——集群故障转移
java·数据库·redis·集群
Java面试题总结2 小时前
mybatis插件
java·tomcat·mybatis