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;
    }
相关推荐
弥巷2 分钟前
【Android】常见滑动冲突场景及解决方案
android·java
浮尘笔记3 分钟前
Go并发编程核心:Mutex和RWMutex的用法
开发语言·后端·golang
散峰而望7 分钟前
C++数组(一)(算法竞赛)
c语言·开发语言·c++·算法·github
间彧15 分钟前
GraalVM 深度解析:下一代 Java 技术平台
java
wjs202427 分钟前
C++ 指针
开发语言
合作小小程序员小小店33 分钟前
网页开发,在线%旧版本旅游管理%系统,基于eclipse,html,css,jquery,servlet,jsp,mysql数据库
java·数据库·servlet·eclipse·jdk·旅游·jsp
20岁30年经验的码农42 分钟前
Java Sentinel流量控制与熔断降级框架详解
java·开发语言·sentinel
程序员西西43 分钟前
SpringBoot轻松整合Sentinel限流
java·spring boot·后端·计算机·程序员
q***46521 小时前
Spring Boot 实战:轻松实现文件上传与下载功能
java·数据库·spring boot
Li_7695321 小时前
10分钟快速入手Spring Cloud Config
java·spring·spring cloud