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;
    }
相关推荐
IT空门:门主3 分钟前
Spring AI的教程,持续更新......
java·人工智能·spring·spring ai
期待のcode9 分钟前
Springboot配置属性绑定
java·spring boot·后端
JANGHIGH11 分钟前
c++ 多线程(二)
开发语言·c++
Acc1oFl4g14 分钟前
详解Java反射
java·开发语言·python
海上彼尚14 分钟前
Go之路 - 6.go的指针
开发语言·后端·golang
Trouvaille ~16 分钟前
【Java篇】存在即不变:深刻解读String类不变的艺术
java·开发语言·javase·stringbuilder·stringbuffer·string类·字符串常量池
lemon_sjdk16 分钟前
java学习——枚举类
java·开发语言·学习
FreeBuf_25 分钟前
Next.js 发布扫描工具:检测并修复受 React2Shell 漏洞(CVE-2025-66478)影响的应用
开发语言·javascript·ecmascript
2022.11.7始学前端34 分钟前
n8n第九节 使用LangChain与Gemini构建带对话记忆的AI助手
java·人工智能·n8n
JosieBook1 小时前
【Spring Boot】Spring Boot调用 WebService 接口的两种方式:动态调用 vs 静态调用 亲测有效
java·spring boot·后端