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;
    }
相关推荐
abcnull3 分钟前
Springboot+Vue2的Web项目小白入门Demo快速学习!
java·elementui·vue·maven·springboot·web·小白
2501_932750269 分钟前
Java IO流基础全面详解:字节流、字符流
java·开发语言
冰暮流星15 分钟前
javascript之默认事件
开发语言·javascript·ecmascript
逸Y 仙X15 分钟前
文章二十二:ElasticSearch EQL事件查询语言
java·大数据·elasticsearch·搜索引擎·全文检索
fengci.18 分钟前
CTF+随机困难题目
android·开发语言·前端·学习·php
liulilittle19 分钟前
LLAMA-CLI 运行千问3.6(R9-7945HX+64G+RTX40608G)
java·前端·llama
l1t21 分钟前
DeepSeek总结的Python 3.14.5 发布候选版本
开发语言·python
后端漫漫23 分钟前
Redis 键值对序列化
java·redis
雪度娃娃25 分钟前
设计模式——单例模式
开发语言·c++·设计模式
Cyber4K26 分钟前
【Python专项】进阶语法-日志分类与分析(2)
开发语言·前端·python