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;
    }
相关推荐
wjs20245 分钟前
PHP 数组
开发语言
jiayong237 分钟前
第 40 课:任务详情抽屉里的编辑 / 删除联动强化
java·开发语言·前端·javascript·vue.js·学习
河阿里8 分钟前
Java八股:面试高频50
java·面试
Rabitebla10 分钟前
【数据结构】实现通讯录:基于C语言动态顺序表
c语言·开发语言·数据结构·算法
小谢小哥12 分钟前
53-熔断降级详解
java·后端·架构
覆东流17 分钟前
第6天:python综合练习——制作简易计算器
开发语言·后端·python
waves浪游25 分钟前
进程间通信(上)
linux·运维·服务器·开发语言·c++
CodeMartain28 分钟前
shardingsphere-spring 实现数据分片(一)
java·后端·spring
圆弧YH28 分钟前
Python→ Bookmark
开发语言·python
hhb_61837 分钟前
C Shell脚本编程与系统管理技术实践指南
java·c语言·开发语言