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;
    }
相关推荐
uzong1 小时前
程序员从大厂回重庆工作一年
java·后端·面试
kyle~1 小时前
C++---value_type 解决泛型编程中的类型信息获取问题
java·开发语言·c++
NiNi_suanfa4 小时前
【Qt】Qt 批量修改同类对象
开发语言·c++·qt
小糖学代码5 小时前
LLM系列:1.python入门:3.布尔型对象
linux·开发语言·python
Data_agent5 小时前
1688获得1688店铺详情API,python请求示例
开发语言·爬虫·python
妖灵翎幺5 小时前
C++ 中的 :: 操作符详解(一切情况)
开发语言·c++·ide
开心香辣派小星6 小时前
23种设计模式-15解释器模式
java·设计模式·解释器模式
Halo_tjn6 小时前
虚拟机相关实验概述
java·开发语言·windows·计算机
star _chen6 小时前
C++实现完美洗牌算法
开发语言·c++·算法
周杰伦fans6 小时前
pycharm之gitignore设置
开发语言·python·pycharm