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;
    }
相关推荐
栗子叶3 分钟前
Java对象创建的过程
java·开发语言·jvm
Amumu1213812 分钟前
React面向组件编程
开发语言·前端·javascript
有一个好名字12 分钟前
力扣-从字符串中移除星号
java·算法·leetcode
IT=>小脑虎13 分钟前
Python零基础衔接进阶知识点【详解版】
开发语言·人工智能·python
wjs202415 分钟前
C 标准库 - `<float.h>》详解
开发语言
zfj32122 分钟前
CyclicBarrier、CountDownLatch、Semaphore 各自的作用和用法区别
java·开发语言·countdownlatch·semaphore·cyclicbarrier
2501_9167665428 分钟前
【JVM】类的加载机制
java·jvm
Sag_ever28 分钟前
Java数组详解
java
张np29 分钟前
java基础-ConcurrentHashMap
java·开发语言
早日退休!!!30 分钟前
进程与线程的上下文加载_保存及内存映射
开发语言