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;
    }
相关推荐
3D探路人14 分钟前
模灵 大模型聚合API 转发流程技术实现
java·大数据·开发语言·前端·人工智能·计算机视觉
l1t32 分钟前
JIT执行python脚本的工具codon安装和测试
开发语言·python
程似锦吖1 小时前
无中生有 之 从0开始写一个动态定时任务管理
java·开发语言
techdashen1 小时前
dial9:给 Tokio 装上“飞行记录仪“
java·数据库·redis
Dxy12393102161 小时前
Python 去除 HTML 标签获取纯文本
开发语言·python·html
ShiJiuD6668889991 小时前
springboot基础篇
java·spring boot·spring
砚底藏山河2 小时前
python、JavaScript 、JAVA,定制化数据服务,助力业务高效落地
java·javascript·python
qq_452396232 小时前
第六篇:《JMeter逻辑控制器:循环、条件和交替执行》
android·java·jmeter
洛的地理研学2 小时前
Python下载并处理MOD13A3植被指数数据
开发语言·python
humcomm2 小时前
Java 新特性2026年5月速览
java·开发语言