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;
    }
相关推荐
‎ദ്ദിᵔ.˛.ᵔ₎6 分钟前
STL 栈 队列
开发语言·c++
砍材农夫9 分钟前
spring-ai 第六模型介绍-聊天模型
java·人工智能·spring
勿忘,瞬间12 分钟前
数据结构—顺序表
java·开发语言
张張40815 分钟前
(域格)环境搭建和编译
c语言·开发语言·python·ai
weixin_4235339920 分钟前
【Windows11离线安装anaconda、python、vscode】
开发语言·vscode·python
fzb5QsS1p22 分钟前
告别重复造轮子,Qt 快速开发脚手架
开发语言·qt·php
航Hang*24 分钟前
第3章:Linux系统安全管理——第2节:部署代理服务
linux·运维·服务器·开发语言·笔记·系统安全
周杰伦fans25 分钟前
C# required 关键字详解
开发语言·网络·c#
༾冬瓜大侠༿27 分钟前
vector
c语言·开发语言·数据结构·c++·算法
liu****40 分钟前
第16届省赛蓝桥杯大赛C/C++大学B组(京津冀)
开发语言·数据结构·c++·算法·蓝桥杯