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;
    }
相关推荐
胡八一几秒前
解决使用PCbuild\build.bat构建python之后,运行pip报错
开发语言·python·pip
ALex_zry1 分钟前
Rust 变量遮蔽 五类典型应用场景
开发语言·后端·rust
hoiii1872 分钟前
MATLAB中离散傅里叶变换(DFT)的实现与分析
开发语言·matlab
灰灰勇闯IT2 分钟前
RN原生模块交互:打通JS与原生的桥梁
开发语言·javascript·交互
乌蒙山连着山外山3 分钟前
linux中查询多个匹配字段
java·linux·服务器
进击的荆棘4 分钟前
C++起始之路——类和对象(中)
开发语言·c++
梦想的旅途25 分钟前
非官方接口下企业微信外部群主动交互:数据传输稳定性优化方案摘要
开发语言·php
沐知全栈开发6 分钟前
Linux 系统目录结构
开发语言
⑩-7 分钟前
@Component
java
xing-xing8 分钟前
Java多版本配置及版本切换(Mac适配)
java·macos