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;
    }
相关推荐
Huangjin007_3 分钟前
【C++11篇(四)】新的类功能、lambda 与包装器详解
开发语言·c++
2zcode4 分钟前
基于MATLAB图像处理的啤酒瓶口缺陷检测系统设计与实现
开发语言·图像处理·matlab
进击的程序猿~9 分钟前
Go Zero源码阅读2
开发语言·后端·golang
hdsoft_huge14 分钟前
JDK系列19:JVM全维度调优实战,内存、元空间、线程栈、GC参数压测调优完整方案
java·开发语言·jvm
曹牧19 分钟前
Eclipse:SVN 回复
java·svn·eclipse
闲猫8 小时前
Spring AI 对接Deepseek ChatModel 聊天对话
java·前端·spring
哥不想学算法8 小时前
【C++】字符串字面量拼接
开发语言·c++
gugucoding10 小时前
21. 【C语言】打包不同类型:结构体
c语言·开发语言
自信的未来10 小时前
JSON 工具|Web Worker 工程化打包 + 语法自动修复 + 多语言代码生成实战
java·前端·json