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;
    }
相关推荐
q567315237 分钟前
企业级 HTTP 代理采购选型:技术评估清单 15 项
开发语言·网络·爬虫·网络协议·http·隧道ip·代理ip
@航空母舰17 分钟前
SpringBoot通过Map实现天然的策略模式
java·spring boot·后端
天天进步201538 分钟前
Python全栈项目--智能办公自动化系统
开发语言·python
Co_Hui43 分钟前
Java 并发编程
java
cm04Z9c911 小时前
C#摸鱼实录——IoC与DI案例详解
开发语言·c#
long3162 小时前
Java 新手入门与实战开发指南
java·开发语言
摩西蒙2 小时前
计算机网络
开发语言·计算机网络·php
山峰哥2 小时前
数据库性能救星:Explain执行计划深度拆解
服务器·开发语言·数据库·sql·启发式算法
执笔画流年呀2 小时前
Linux搭建Java项目部署环境
java·linux·运维
程序员天天困2 小时前
Arthas ognl 表达式从入门到实战:掌握在线调试最强的表达式引擎
java·jvm·后端