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;
    }
相关推荐
二哈喇子!33 分钟前
BOM模型
开发语言·前端·javascript·bom
二哈喇子!34 分钟前
Java开发工具——IDEA(修改全局配置,提升工作效率)
java·编辑器·intellij-idea
二哈喇子!41 分钟前
空指针异常
开发语言
强子感冒了1 小时前
Java网络编程学习笔记,从网络编程三要素到TCP/UDP协议
java·网络·学习
咚为1 小时前
Rust Print 终极指南:从底层原理到全场景实战
开发语言·后端·rust
二哈喇子!1 小时前
SpringBoot项目右上角选择ProjectNameApplication的配置
java·spring boot
%xiao Q1 小时前
GESP C++五级-202406
android·开发语言·c++
Psycho_MrZhang1 小时前
Neo4j Python SDK手册
开发语言·python·neo4j
Traced back1 小时前
# C# + SQL Server 实现自动清理功能的完整方案:按数量与按日期双模式
开发语言·c#
sin22011 小时前
MyBatis的执行流程
java·开发语言·mybatis