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;
    }
相关推荐
AI人工智能+电脑小能手几秒前
【大白话说Java面试题】【Java基础篇】第5题:HashMap的底层原理是什么
java·开发语言·数据结构·后端·面试·hash-index·hash
旷世奇才李先生1 分钟前
Java微服务实战:Spring Cloud Alibaba架构优化(从单体到分布式高可用)
java·微服务·架构
小成202303202651 分钟前
数据结构(整理常见结构总结到树层级)
java·c语言·数据结构·c++·链表
谢谢 啊sir2 分钟前
L1-120 智慧文本编辑器 - java
java·开发语言
sycmancia7 分钟前
Qt——缓冲区操作与目录操作
开发语言·qt
weisian1517 分钟前
进阶篇-LangChain篇-15--高级Agent架构—复杂任务拆解(Plan-and-Execute架构)和多智能体协作(LangGraph)
java·架构·langchain·langgraph·planexecute架构
AIGC设计所7 分钟前
网络安全SRC漏洞挖掘学习路线 - 第四期:常见漏洞挖掘实操,实现首次挖洞突破
开发语言·网络·学习·安全·web安全
就叫飞六吧8 分钟前
在线考试翻页抓取题目导出js
开发语言·前端·javascript
凤年徐8 分钟前
自动化构建工具:make 与 Makefile
android·java·linux·自动化
neo33019 分钟前
debian13 编译源码qt5.15.18
开发语言·qt