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;
    }
相关推荐
轩情吖7 分钟前
MySQL之表的增删查改
android·开发语言·c++·后端·mysql·adb·
我真会写代码9 分钟前
深入理解Java JVM:架构、核心机制与实战调优指南
java·jvm·架构
2301_7938046911 分钟前
C++与硬件交互编程
开发语言·c++·算法
6+h15 分钟前
【java IO】BIO、NIO、AIO 全面对比
java·python·nio
csbysj202017 分钟前
jEasyUI 转换 HTML 表格为数据网格
开发语言
历程里程碑21 分钟前
39. 从零实现UDP服务器实战(带源码) V1版本 - Echo server
服务器·开发语言·网络·c++·网络协议·udp·php
Book思议-22 分钟前
【数据结构实战】:基于C语言单链表实现红旗渠景区年卡信息管理系统
c语言·开发语言·数据结构
Chase_______22 分钟前
【快速入手 Python 基础 | 第1章】:数据存储与运算
开发语言·python
骇客野人24 分钟前
Java springboot里注解大全和使用指南
java·开发语言·spring boot
用户83071968408225 分钟前
Spring Boot 启动报错:OpenFeign 隐性循环依赖,排查了整整一下午
java·spring boot·spring cloud