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;
    }
相关推荐
m0_748240444 小时前
Laravel5.6核心更新全解析
开发语言·php
鹿角片ljp4 小时前
Java网络编程入门:从Socket到多线程服务器
java·服务器·网络
曹牧4 小时前
C#:Obsolete
开发语言·c#
我是苏苏4 小时前
Web开发:使用C#的System.Drawing.Common将png图片转化为icon图片
开发语言·c#
走进IT4 小时前
DDD项目分层结构说明
java
橙露4 小时前
嵌入式实时操作系统 FreeRTOS:任务调度与信号量的核心应用
java·大数据·服务器
愚公移码4 小时前
蓝凌EKP产品:关联机制浅析
java·服务器·前端
冬奇Lab4 小时前
【Kotlin系列11】协程原理与实战(下):Flow与Channel驯服异步数据流
android·开发语言·kotlin
好大哥呀4 小时前
如何在手机上运行Python程序
开发语言·python·智能手机
阿蒙Amon4 小时前
C#每日面试题-is和as的区别
java·开发语言·c#