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;
    }
相关推荐
xyq202413 小时前
Python 日期和时间处理指南
开发语言
闻哥13 小时前
深入剖析Redis数据类型与底层数据结构
java·jvm·数据结构·spring boot·redis·面试·wpf
xyq202413 小时前
Bootstrap4 折叠组件详解
开发语言
虾..13 小时前
Linux 基于TCP实现服务端客户端通信(多进程/多线程版)
java·服务器·tcp/ip
星辰_mya14 小时前
CompletableFuture:异步编程的“智能机械臂”
java·开发语言·面试
阿蒙Amon14 小时前
C#常用类库-详解AutoMapper
开发语言·c#
一见14 小时前
WorkBuddy安装Skill的方法
android·java·javascript
沐知全栈开发14 小时前
C 头文件
开发语言
yuuki23323314 小时前
【C++ 智能指针全解析】从内存泄漏痛点到 RAII + unique/shared/weak_ptr 手撕实现
开发语言·c++
悟空码字14 小时前
SpringBoot + 腾讯地图实战:打造全能型地理位置服务平台,开箱即用!
java·spring boot·后端