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;
    }
相关推荐
常利兵7 分钟前
解锁Kotlin:数据类与密封类的奇妙之旅
android·开发语言·kotlin
来自远方的老作者9 分钟前
第7章 运算符-7.1 算术运算符
开发语言·数据结构·python·算法·算术运算符
MwEUwQ3Gx17 分钟前
PHP 异步与多线程 从 TrueAsync 展望未来
开发语言·php
pl4H522a631 分钟前
Python 高效实现 Excel 转 TXT 文本
java·python·excel
稻草猫.35 分钟前
Spring事务操作全解析
java·数据库·后端·spring
不会写DN41 分钟前
Go中如何跨语言实现传输? - GRPC
开发语言·qt·golang
她说..1 小时前
Java 基本数据类型高频面试题
java·开发语言·jvm·spring boot
y = xⁿ1 小时前
小林coding:HashMap的原理,ConcurrentHashMap实现逻辑,1.8并发是如何超越1.7的
java·面试·hash
white-persist1 小时前
【vulhub weblogic CVE-2017-10271漏洞复现】vulhub weblogic CVE-2017-10271漏洞复现详细解析
java·运维·服务器·网络·数据库·算法·安全
zzginfo1 小时前
JavaScript 解构赋值
开发语言·javascript·ecmascript