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;
    }
相关推荐
kuntli5 分钟前
Spring Bean生命周期全解析
java
ok_hahaha6 分钟前
java从头开始-苍穹外卖-day06-微信小程序开发-微信登录和商品浏览
java·微信·微信小程序·小程序
m0_706653239 分钟前
使用C-Free进行浮点变量的四则运算指南
c语言·开发语言
Java面试题总结14 分钟前
Spring @Validated失效?原因、排查与高效解决方案全解析
java·spring boot·spring
剑锋所指,所向披靡!33 分钟前
MySQL数据的增删改查
java·数据库·mysql
Villiam_AY37 分钟前
一次 DNS 端口引发的代理网络和公司内网冲突问题
java·服务器·数据库
dgvri44 分钟前
比较Spring AOP和AspectJ
java
eggwyw1 小时前
springboot和springframework版本依赖关系
java·spring boot·后端
流水迢迢lst2 小时前
靶场练习day15--php反序列化
开发语言·php
于先生吖2 小时前
国际版JAVA婚恋交友系统源码:多语言适配,可商用的跨境婚恋解决方案
java·大数据·交友