Java计算文件MD5值(支持大文件) package com.hthl.xxtd; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.security.MessageDigest; import org.apache.commons.codec.binary.Hex; import org.apache.commons.codec.digest.DigestUtils; /** *MD5计算工具 xuxile 2017-09-13 */ public class Md5CaculateUtil { /** * 获取一个文件的md5值(可处理大文件) * @return md5 value */ public static String getMD5(File file) { FileInputStream fileInputStream = null; try { MessageDigest MD5 = MessageDigest.getInstance("MD5"); fileInputStream = new FileInputStream(file); byte[] buffer = new byte[8192]; int length; while ((length = fileInputStream.read(buffer)) != -1) { MD5.update(buffer, 0, length); } return new String(Hex.encodeHex(MD5.digest())); } catch (Exception e) { e.printStackTrace(); return null; } finally { try { if (fileInputStream != null){ fileInputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 求一个字符串的md5值 * @param target 字符串 * @return md5 value */ public static String MD5(String target) { return DigestUtils.md5Hex(target); } public static void main(String[] args) { long beginTime = System.currentTimeMillis(); File file = new File("D:/1/pdi-ce-7.0.0.0-24.zip"); String md5 = getMD5(file); long endTime = System.currentTimeMillis(); System.out.println("MD5:" + md5 + "\n 耗时:" + ((endTime - beginTime) / 1000) + "s"); } }
package com.hthl.xxtd;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
/**
*MD5计算工具 xuxile 2017-09-13
*/
public class Md5CaculateUtil {
/**
* 获取一个文件的md5值(可处理大文件)
* @return md5 value
*/
public static String getMD5(File file) {
FileInputStream fileInputStream = null;
try {
MessageDigest MD5 = MessageDigest.getInstance("MD5");
fileInputStream = new FileInputStream(file);
byte[] buffer = new byte[8192];
int length;
while ((length = fileInputStream.read(buffer)) != -1) {
MD5.update(buffer, 0, length);
- weixin_378314482022-08-09测试了下载后可以使用
- 粉丝: 1
- 资源: 2
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- wav2lip-gan.pth 资源
- 广告点击率预测中常用的criteo数据集
- 三菱PLC例程源码fx2nplc与三台丹佛斯变频器458通讯定位.定尺源程序
- 三菱PLC例程源码FX2NPLC与安川G7变频器通讯(原创)有注释,MEMOBUS的写方式
- 三菱PLC例程源码FX2NPLC与A700变频器RUT通讯
- 小智搜搜-必备搜索工具,搜索快启更胜一筹
- 三菱PLC例程源码FX2NPLC和变频器,F930通讯控制的程序有注解拉丝机程序
- 三菱PLC例程源码FX2NPLC+2AD+4ADTC+2DA+电阻尺+编码器+PID温控
- 安装和配置指引,通俗易懂
- 三菱PLC例程源码FX2NPLC+2AD+4ADTC+2DA+电阻尺+编码器+PID温控完整注释程式+UG330人机