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;
    }
相关推荐
lly202406几秒前
SOAP 简介
开发语言
重庆小透明11 分钟前
【面试问题】java字节八股部分
java·面试·职场和发展
小王不爱笑13212 分钟前
Java 对象拷贝(浅拷贝 / 深拷贝)
java·开发语言·python
架构师沉默15 分钟前
程序员真的要失业了吗?
java·后端·架构
小王不爱笑13223 分钟前
SpringBoot 自动装配深度解析:从底层原理到自定义 starter 实战(含源码断点调试)
java·spring boot·mybatis
森林里的程序猿猿26 分钟前
Spring Aop底层源码实现(一)
java·后端·spring
ℳ๓₯㎕.空城旧梦31 分钟前
C++中的解释器模式
开发语言·c++·算法
JdayStudy39 分钟前
SIR 网络传播仿真软件说明书
开发语言·网络·php
有点傻的小可爱1 小时前
【MATLAB】新安装并口如何实现能通过PTB启用?
开发语言·windows·经验分享·matlab
符哥20081 小时前
充电桩 WiFi 局域网配网(Android/Kotlin)流程、指令及实例说明文档
android·开发语言·kotlin