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;
    }
相关推荐
gelald6 小时前
Spring Boot - 自动配置原理
java·spring boot·后端
hssfscv6 小时前
软件设计师下午题六——Java的各种设计模式
java·算法·设计模式
希望永不加班6 小时前
SpringBoot 集成测试:@SpringBootTest 与 MockMvc
java·spring boot·后端·log4j·集成测试
enAn_6 小时前
对照片和视频文件名,程序追加日期,直观看
java·maven
十五年专注C++开发6 小时前
Oat++: 一个轻量级、高性能、零依赖的 C++ Web 框架
开发语言·c++·web服务·oatpp
yaaakaaang7 小时前
六、适配器模式
java·适配器模式
陈天伟教授7 小时前
心电心音同步分析-案例:原型设计一
开发语言·人工智能·python·语言模型·架构
Allen_LVyingbo7 小时前
量子计算Dirac Notation基本教学—从零基础到读懂量子信息论文(下)
开发语言·人工智能·python·数学建模·量子计算
bobasyu7 小时前
Claude Code 源码笔记 -- queryLoop
java·笔记·spring
wjs20247 小时前
Ruby File 类和方法
开发语言