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;
    }
相关推荐
indexsunny9 分钟前
互联网大厂Java面试实战:从Spring Boot到微服务架构的技术问答解析
java·spring boot·redis·微服务·kafka·jwt·flyway
toooooop89 分钟前
php BC MATH扩展函数计算精度-第三个参数
开发语言·php
蓁蓁啊13 分钟前
C/C++编译链接全解析——gcc/g++与ld链接器使用误区
java·c语言·开发语言·c++·物联网
sheji341616 分钟前
【开题答辩全过程】以 基于SpringBoot的疗养院管理系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
weixin_3077791320 分钟前
C#实现两个DocumentDB实例之间同步数据
开发语言·数据库·c#·云计算
foundbug99926 分钟前
基于C#的OPC DA客户端实现源码解析
开发语言·c#
tb_first29 分钟前
万字超详细苍穹外卖学习笔记2
java·jvm·数据库·spring·tomcat·maven
yuezhilangniao33 分钟前
Next.js 项目运维手册-含-常用命令-常见场景
运维·开发语言·reactjs
短剑重铸之日39 分钟前
《设计模式》第六篇:装饰器模式
java·后端·设计模式·装饰器模式