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;
    }
相关推荐
K***658912 小时前
Tomcat下载,安装,配置终极版(2024)
java·tomcat
x***381612 小时前
springboot整合最新版minio和minio的安装(完整教程,新人必看)
java·spring boot·后端
I***t71612 小时前
Go环境搭建(vscode调试)
开发语言·vscode·golang
Mr_Xuhhh12 小时前
pytest -- fixture
开发语言·python·pytest
通往曙光的路上12 小时前
陪玩小项目努力
java
w***48112 小时前
Maven Spring框架依赖包
java·spring·maven
未来之窗软件服务12 小时前
幽冥大陆(三十八)P50酒店门锁SDK C#仙盟插件——东方仙盟筑基期
开发语言·单片机·c#·东方仙盟·东方仙盟sdk·东方仙盟vos智能浏览器
汤姆yu12 小时前
基于springboot的乡村信息建设管理系统
java·spring boot·后端
Halo_tjn12 小时前
Java List集合
java·windows·计算机
wzm—12 小时前
C#获取每年节假日
开发语言·c#