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;
    }
相关推荐
笨蛋不要掉眼泪1 分钟前
Java并发编程:Executors框架类深度解析
java·开发语言·并发
南极企鹅9 分钟前
深入理解 MVCC:数据库并发控制的基石
java·数据库·mysql
凯瑟琳.奥古斯特43 分钟前
力扣1235:加权区间调度最优解
java·python·算法·leetcode·职场和发展
_童年的回忆_1 小时前
【php】在linux下PHP安装amqp扩展
linux·开发语言·php
想不到ID了1 小时前
第八篇: 登录注册功能实现
java·javascript
AIMath~1 小时前
python中的uv命令揭秘
开发语言·python·uv
码语智行1 小时前
shp文件生成
java
弹简特1 小时前
【零基础学Python】06-Python模块和包、异常处理、文件常用操作
开发语言·python
x***r1511 小时前
Postman-win64-7.2.2-Setup安装步骤详解(附API接口测试与参数配置教程)
开发语言·lua
plainGeekDev1 小时前
AlertDialog → DialogFragment
android·java·kotlin