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;
    }
相关推荐
Ulyanov5 小时前
高保真单脉冲雷达导引头回波生成:Python建模与实践
开发语言·python·仿真·系统设计·单脉冲雷达
t***44235 小时前
【Springboot3+vue3】从零到一搭建Springboot3+vue3前后端分离项目之后端环境搭建
java
daidaidaiyu5 小时前
SpringCloud 微服务实现一则 (Eureka + Hystrix)
java·spring
小哥不太逍遥6 小时前
Technical Report 2024
java·服务器·前端
阿猿收手吧!6 小时前
【C++】jthread:优雅终止线程新方案
开发语言·c++
lly2024066 小时前
《JavaScript 实例》
开发语言
edisao6 小时前
序幕-内部审计备忘录
java·jvm·算法
十五年专注C++开发6 小时前
C++中各平台表示Debug的宏
开发语言·c++·debug
shehuiyuelaiyuehao6 小时前
22Java对象的比较
java·python·算法
张小凡vip6 小时前
Python异步编程实战:基于async/await的高并发实现
开发语言·python