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;
    }
相关推荐
Edward1111111124 分钟前
tomcat_servlet
java·servlet·tomcat
短剑重铸之日36 分钟前
SpringBoot声明式事务的源码解析
java·后端·spring·springboot
李白的粉38 分钟前
基于springboot的银行客户管理系统(全套)
java·spring boot·毕业设计·课程设计·源代码·银行客户管理系统
JIngJaneIL41 分钟前
基于springboot + vue房屋租赁管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
努力的小南1 小时前
Java字节码文件常量池索引两个问题
java·常量池·字节码文件·黑马jvm字节码文件常量池索引
期待のcode1 小时前
Java的抽象类和接口
java·开发语言
while(1){yan}1 小时前
SpringDI
java·jvm·spring·java-ee
陈平安安1 小时前
设计一个秒杀功能
java·数据库·sql
TAEHENGV1 小时前
基本设置模块 Cordova 与 OpenHarmony 混合开发实战
android·java·数据库
wadesir1 小时前
Go语言中高效读取数据(详解io包的ReadAll函数用法)
开发语言·后端·golang