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;
    }
相关推荐
书源丶18 分钟前
四十三、网络编程(下)——TCP 编程与 HTTP 入门
java·网络·tcp/ip·http
木井巳20 分钟前
【递归算法】单词搜索
java·算法·leetcode·决策树·深度优先
阿豪只会阿巴39 分钟前
【没事学点啥】TurboBlog轻量级个人博客项目——Turbo Blog 项目学习与上线指南
开发语言·python·学习·状态模式
幸运的大号暖贴41 分钟前
解决Vibe Coding时Idea经常不自动git add问题
java·人工智能·git·intellij-idea·claudecode·opencode
m0_716255001 小时前
第一部分 数据开发 面试全题 模拟口述版(自问自答)
java·数据库·面试
L-影1 小时前
常见的 ORM 工具
开发语言·数据库·fastapi·orm
飞Link1 小时前
构筑你的数字第二大脑:Obsidian 深度解析与配置指南
开发语言·python
SuperherRo1 小时前
服务攻防-Java组件安全&FastJson&高版本JNDI&不出网C3P0&编码绕WAF&写入文件CI链
java·安全·fastjson·waf·不出网·高版本·写入文件
丑八怪大丑1 小时前
SQL数据类型
java·数据库·sql
Nyarlathotep01131 小时前
并发集合类(3):LinkedBlockingQueue
java·后端