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;
    }
相关推荐
WooaiJava3 分钟前
流式TTS音频播放项目 - 面试问答(后端)
java·开发语言
奥升新能源平台4 分钟前
奥升充电|充电站用户分层分析与精细化运营策略研究
java·大数据·能源
新缸中之脑12 分钟前
开发AI代理必备的8个Python 库
开发语言·人工智能·python
暴走十八步15 分钟前
PHP+vscode开启调试debug
开发语言·vscode·php
梵得儿SHI17 分钟前
(第十篇)Spring AI 核心技术攻坚全梳理:企业级能力矩阵 + 四大技术栈攻坚 + 性能优化 Checklist + 实战项目预告
java·人工智能·spring·rag·企业级ai应用·springai技术体系·多模态和安全防护
一路向北⁢17 分钟前
Spring Boot 3 整合 SSE (Server-Sent Events) 企业级最佳实践(三)
java·spring boot·后端·sse
郝学胜-神的一滴18 分钟前
Python 列表 vs 数组:深入解析与最佳选择指南
开发语言·python·程序人生
杜子不疼.19 分钟前
基于ATVC模板库的Ascend C Vector算子快速开发指南
c语言·开发语言·mfc
MSTcheng.21 分钟前
【C++】C++11新特性(三)
开发语言·c++·c++11
learning-striving22 分钟前
kali连不上网解决方法
linux·开发语言·网络·php·kali