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;
    }
相关推荐
twj_one11 小时前
Arthas使用
java
lizz3111 小时前
C++模板编程:从入门到精通
java·开发语言·c++
shoubepatien12 小时前
JAVA -- 05
java·开发语言
寰天柚子12 小时前
Java并发编程中的线程安全问题与解决方案全解析
java·开发语言·python
沐知全栈开发12 小时前
Bootstrap 下拉菜单:设计与实现指南
开发语言
memgLIFE12 小时前
Springboot 分层结构
java·spring boot·spring
shoubepatien12 小时前
JAVA -- 08
java·后端·intellij-idea
kong790692812 小时前
Java新特性-(二)Java基础语法
java·新特性·java 基础语法
yangminlei12 小时前
springboot pom.xml配置文件详细解析
java·spring boot·后端
Evand J12 小时前
【MATLAB例程】多锚点RSSI定位和基站选择方法,基于GDOP、基站距离等因素。以Wi-Fi定位为例,附下载链接
开发语言·matlab·定位·gdop·rssi