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;
    }
相关推荐
wjs20246 分钟前
JavaScript 条件语句
开发语言
lulu121654407825 分钟前
Claude Code Harness架构技术深度解析:生产级AI Agent工程化实践
java·人工智能·python·ai编程
阿里加多26 分钟前
第 1 章:Go 并发编程概述
java·开发语言·数据库·spring·golang
一 乐31 分钟前
物流信息管理|基于springboot + vue物流信息管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·物流信息管理系统
2301_792674861 小时前
java学习day29(juc)
java·开发语言·学习
周末也要写八哥1 小时前
MATLAB R2025a超详细下载与安装教程(附安装包)
开发语言·matlab
希望永不加班2 小时前
SpringBoot 自动配置类加载顺序与优先级
java·spring boot·后端·spring·mybatis
纸鸢|2 小时前
从“一锤子买卖“到“持续价值共生“:物联网软件产品如何做到“叫好又叫座“
java·物联网·struts
blog_wanghao2 小时前
基于Qt的串口调试助手
开发语言·qt
云霄IT3 小时前
安卓开发之java转dex再转smali
android·java·python