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;
    }
相关推荐
2401_838472512 分钟前
C++模拟器开发实践
开发语言·c++·算法
初九之潜龙勿用3 分钟前
C# 操作Word模拟解析HTML标记之背景色
开发语言·c#·word·.net·office
froginwe119 分钟前
MySQL UNION 操作详解
开发语言
ruxshui10 分钟前
Python多线程环境下连接对象的线程安全管理规范
开发语言·数据库·python·sql
雨季66610 分钟前
Flutter 三端应用实战:OpenHarmony 简易点击计数器与循环颜色反馈器开发指南
开发语言·flutter·ui·ecmascript·dart
望眼欲穿的程序猿11 分钟前
Ai8051U+DHT11温湿度!
java·开发语言
一只大马猴呀12 分钟前
IntelliJ IDEA 中启动项目不显示端口号
java·ide·intellij-idea
xcs1940513 分钟前
前端 项目构建问题 \node_modules\loader-runner\lib\loadLoader.js
开发语言·前端·javascript
一人の梅雨13 分钟前
VVIC图片搜索接口进阶实战:服装批发场景下的精准识图与批量调度方案
开发语言·机器学习·php
s1hiyu16 分钟前
实时控制系统验证
开发语言·c++·算法