import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
/**
* Created on 2013-5-22
* <p>Description: 获取文件MD5工具类</p>
* @version 1.0
*/
public class MD5Util {
static MessageDigest MD5 = null;
static {
try {
MD5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException ne) {
ne.printStackTrace();
}
}
/**
* 对一个文件获取md5值
* @return md5串
*/
public static String getMD5(File file) {
FileInputStream fileInputStream = null;
try {
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 (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException 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 fileZIP = new File("E:\\资料\\ws.rar");
String md5=getMD5(fileZIP);
long endTime =System.currentTimeMillis();
System.out.println("MD5:"+md5+"\n time:"+((endTime-beginTime)/1000)+"s");
}
}
这个方法可以解决获取大文件MD5,不会出现内存溢出
分享到:
相关推荐
'获取文件MD5值的函数 Public Shared Function _MD5(ByVal strSource As String) As String Dim fstream As New FileStream(strSource, FileMode.Open, FileAccess.Read) Dim dataToHash(fstream.Length - 1) As...
VC/MFC 获取文件MD5值 获取大文件MD5值 完整项目
源码功能: windows 系统通过命令行获取大文件MD5值。 实测可正常获取大于5GB文件的MD5值。 编译工具: VS2017,字符集随意。
文件不需要上传到服务器就能获得文件md5
用于获取文件的MD5值,用于获取文件的MD5值,
Flex 获取文件MD5值插件,调用格式为: var md5:String=by.blooddy.crypto.MD5.hashBytes(file.data);
delphi 获取文件 md5 吗,带源代码,放心使用。
使用VB.NET,获取文件的MD5。。。。。。。。。。。。。。
java源码 java 获取文件md5值(用于检测文件完整性) .
Java计算文件MD5值(支持大文件) package com.hthl.xxtd; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.security.MessageDigest; import org....
vc++获取文件Md5的源代码,大家可以参考下。
C# 读取文件MD5值 目前我用于读取软件主程序文件的Md5值,以校验是否需要升级 Visual Studio 2008专业版编译
获取目录下所有文件MD5值,获取目录下所有文件MD5值.
只需要在命令控制行输入"md5 文件名"就可以获取md5值,简单易用并且保证md5值是准确的! 文件名可以是全路径,也可以是相对路径。 本人制作的简单工具,比如Windows自带的命令方面多了. 2022-08-18 补充,因为是用...
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] retVal = md5.ComputeHash(file); file.Close(); StringBuilder sb = new StringBuilder(); ...
NULL 博文链接:https://hw1287789687.iteye.com/blog/1924190
VB.net 2012 获取文件MD5 速度和Hash媲美了...
md5源码-获取文件MD5值
C# 下获取文件MD5值 例子