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;
    }
相关推荐
贵沫末4 分钟前
Python——图像处理项目Conda环境搭建
开发语言·python·conda
白日梦想家6814 分钟前
定时器实战避坑+高级用法,从入门到精通
开发语言·前端·javascript
white-persist8 分钟前
逆向入门经典题:从 IDA 反编译坑点到 Python 解题详细分析解释
c语言·开发语言·数据结构·python·算法·逆向·安全架构
是宇写的啊8 分钟前
MyBaties
java·开发语言·mybatis
钝挫力PROGRAMER11 分钟前
程序中事件机制的实现
java·后端·python·软件工程
-凌凌漆-12 分钟前
【Qt】const QString &与QString的区别
开发语言·qt
程序员威哥15 分钟前
Java调用YOLO模型性能优化实战:CPU/GPU加速与内存优化全指南
java·人工智能·后端
Drone_xjw16 分钟前
Qt QTableView 表头变白问题(Kylin/UKUI系统)原因分析与解决方案
开发语言·qt·kylin
mabing99316 分钟前
Qt 实现自定义分段控制器
开发语言·qt
爱喝雪碧的可乐20 分钟前
【Redis 毁灭计划】7 大高危操作打崩线上服务!从缓存雪崩到数据库宕机,90% 程序员都踩过的坑
开发语言·网络·redis·php