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 小时前
6.3 主函数流程剖析
开发语言·php
TheBestRucy5 小时前
Python 九阳神功:从零筑基到线程飞升
服务器·开发语言·网络·人工智能·python
研☆香5 小时前
聊一聊前端的常见字 关键字
开发语言·前端·javascript
xier_ran5 小时前
【infra之路】GPU 存储层次总结:L1 / L2 / HBM
java·网络·数据库
wuminyu5 小时前
JDK21中虚拟线程和FFM协同实现高并发源码剖析
java·linux·c语言·jvm·c++
草莓橙子碗5 小时前
Claude 使用指南
开发语言·python
SWAGGY..5 小时前
【C++进阶】:(1)继承机制详解
java·jvm·c++
longxiaozhang66 小时前
C# Array类:数组其实没那么难
开发语言·c#
码匠许师傅6 小时前
【C++ 面试真题】27. 聊聊 C++ 的内存泄漏与内存布局
java·c++·面试
山甫aa6 小时前
Javaweb---MyBatis增删改查
java·数据库·后端·mybatis·springboot