Java读取文件MD5的两种方案
2015-08-09 10:39
465 查看
最近要弄一下Java读取文件MD5,注意这里是小文件,网上找了一下,有两种解决方案,一种是用MessageDigest来实现,另种是用org.apache.commons.codec.digest来实现,代码如下:
[java] view
plaincopy
package cdm;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import org.apache.commons.codec.digest.*;
import org.apache.commons.io.IOUtils;
public class testMD5 {
public static String getMd5ByFile(File file) throws FileNotFoundException {
String value = null;
FileInputStream in = new FileInputStream(file);
try {
MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(byteBuffer);
BigInteger bi = new BigInteger(1, md5.digest());
value = bi.toString(16);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return value;
}
public static void main(String[] args) throws IOException {
String path="E:\\commons-codec-1.9-bin.zip";
String v = getMd5ByFile(new File(path));
System.out.println("MD5:"+v.toUpperCase());
FileInputStream fis= new FileInputStream(path);
String md5 = DigestUtils.md5Hex(IOUtils.toByteArray(fis));
IOUtils.closeQuietly(fis);
System.out.println("MD5:"+md5);
//System.out.println("MD5:"+DigestUtils.md5Hex("WANGQIUYUN"));
}
}
运行:
结果在官网提供的MD5一致。
附commons-codec-1.9.jar下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi
如果要处理过G的文件,请参照:/article/6358854.html
[java] view
plaincopy
package cdm;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import org.apache.commons.codec.digest.*;
import org.apache.commons.io.IOUtils;
public class testMD5 {
public static String getMd5ByFile(File file) throws FileNotFoundException {
String value = null;
FileInputStream in = new FileInputStream(file);
try {
MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(byteBuffer);
BigInteger bi = new BigInteger(1, md5.digest());
value = bi.toString(16);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return value;
}
public static void main(String[] args) throws IOException {
String path="E:\\commons-codec-1.9-bin.zip";
String v = getMd5ByFile(new File(path));
System.out.println("MD5:"+v.toUpperCase());
FileInputStream fis= new FileInputStream(path);
String md5 = DigestUtils.md5Hex(IOUtils.toByteArray(fis));
IOUtils.closeQuietly(fis);
System.out.println("MD5:"+md5);
//System.out.println("MD5:"+DigestUtils.md5Hex("WANGQIUYUN"));
}
}
运行:
结果在官网提供的MD5一致。
附commons-codec-1.9.jar下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi
如果要处理过G的文件,请参照:/article/6358854.html
相关文章推荐
- java中图的两种存储方式
- Java学习之路:不走弯路,就是捷径
- 编码剖析Spring装配基本属性的原理
- JAVA环境变量配置
- Java字符串和输入流的相互转换
- Java中方法名和类名的规范
- java 单例模式
- Spring整合Hibernate遇到的错误
- JAVA环境变量配置
- Java中wait,notify方法
- java i/o操作
- MyEclipse 10 部署项目时报错,抛出java.lang.NullPointerException异常的解决方法
- Spring核心之Ioc容器走读笔记
- 【Java多线程】之三:Java Thread Join Example
- 安装JDK:找不到或无法加载主类 com.sun.tools.javac.Main
- elipse安装Genymotion插件时候输入网址没有显示可以下载的
- 【Java】字符串模板替换
- Java内部类
- 【Java多线程】之二:Java Thread Sleep Example
- webservice之jdk自带wsimport