`

java 获取文件MD5值

阅读更多

java 中,如何获取文件的MD5值呢?如何比较两个文件是否完全相同呢?

/**
	 * Get MD5 of one file:hex string,test OK!
	 * 
	 * @param file
	 * @return
	 */
	public static String getFileMD5(File file) {
		if (!file.exists() || !file.isFile()) {
			return null;
		}
		MessageDigest digest = null;
		FileInputStream in = null;
		byte buffer[] = new byte[1024];
		int len;
		try {
			digest = MessageDigest.getInstance("MD5");
			in = new FileInputStream(file);
			while ((len = in.read(buffer, 0, 1024)) != NEGATIVE_ONE) {
				digest.update(buffer, 0, len);
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		BigInteger bigInt = new BigInteger(1, digest.digest());
		return bigInt.toString(16);
	}

	/***
	 * Get MD5 of one file!test ok!
	 * 
	 * @param filepath
	 * @return
	 */
	public static String getFileMD5(String filepath) {
		File file = new File(filepath);
		return getFileMD5(file);
	}

	/**
	 * MD5 encrypt,test ok
	 * 
	 * @param data
	 * @return byte[]
	 * @throws Exception
	 */
	public static byte[] encryptMD5(byte[] data) throws Exception {

		MessageDigest md5 = MessageDigest.getInstance(SystemUtil.KEY_MD5);
		md5.update(data);
		return md5.digest();
	}

	public static byte[] encryptMD5(String data) throws Exception {
		return encryptMD5(data.getBytes(SystemUtil.CHARSET_ISO88591));
	}
	/***
	 * compare two file by Md5
	 * 
	 * @param file1
	 * @param file2
	 * @return
	 */
	public static boolean isSameMd5(File file1,File file2){
		String md5_1=SystemUtil.getFileMD5(file1);
		String md5_2=SystemUtil.getFileMD5(file2);
		return md5_1.equals(md5_2);
	}
	/***
	 * compare two file by Md5
	 * 
	 * @param filepath1
	 * @param filepath2
	 * @return
	 */
	public static boolean isSameMd5(String filepath1,String filepath2){
		File file1=new File(filepath1);
		File file2=new File(filepath2);
		return isSameMd5(file1, file2);
	}

 测试(使用junit):

@Test
	public void test_getFileMD5() throws Exception{
		String filepath="D:\\download\\3_尚学堂_UML概览.avi";
//		File file=new File(filepath);
		String md5_1=SystemUtil.getFileMD5(filepath);
		System.out.println(md5_1);
		
		byte[]bytes=FileUtils.readBytes4file(filepath);
		byte[]md5=SystemUtil.encryptMD5(bytes);
		String md5_2=SystemUtil.toHexString(md5);
		System.out.println(md5_2);
		Assert.assertEquals(md5_1, md5_2);
	}

 

分享到:
评论
12 楼 3344606096 2018-02-04  
6ee55e422428f6c8e502747c12d9fffe
11 楼 hw1287789687 2017-12-16  
gmxy 写道
a116475939 写道
指正一个错误:
BigInteger bigInt = new BigInteger(1, digest.digest());

此处如果获取的md5是以0开头的,
那么bigInt将变成没有0的15位。

有什么解决办法吗

谢谢!
已修正:
 /**
     * Get MD5 of one file:hex string,test OK!
     *
     * @param file
     * @return : hex string
     */
    public static String getFileMD5(File file) {
        if (!file.exists() || !file.isFile()) {
            return null;
        }
        MessageDigest digest = null;
        FileInputStream in = null;
        byte buffer[] = new byte[1024];
        int len;
        try {
            digest = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);
            while ((len = in.read(buffer, 0, 1024)) != NEGATIVE_ONE) {
                digest.update(buffer, 0, len);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
//        BigInteger bigInt = new BigInteger(1, digest.digest());
        return SystemHWUtil.toHexString(digest.digest());// bigInt.toString(16);
    }


toHexString 方法见SystemUtil
中,也可以参考 https://github.com/liuyu520/io0007 中的com/common/util/SystemHWUtil.java
10 楼 gmxy 2017-11-23  
a116475939 写道
指正一个错误:
BigInteger bigInt = new BigInteger(1, digest.digest());

此处如果获取的md5是以0开头的,
那么bigInt将变成没有0的15位。

有什么解决办法吗
9 楼 a116475939 2017-09-22  
指正一个错误:
BigInteger bigInt = new BigInteger(1, digest.digest());

此处如果获取的md5是以0开头的,
那么bigInt将变成没有0的15位。
8 楼 leandzgc 2017-01-16  
支持支持支持!!!很厉害!!!被人盗链到阿里云上面了,很尴尬!!!
7 楼 hw1287789687 2014-08-27  

com.io.hw.file.util.FileUtils;
com.String.widget.util.ValueWidget
com.time.util.TimeHWUtil
下载地址:http://pan.baidu.com/s/1o6LoNrk

http://www.yunmasoft.com
6 楼 huangjnjavaIt 2014-08-18  
hw1287789687 写道
huangjnjavaIt 写道
楼主,你好,systemUtil.java中引用的其他文件能否提供一下呢?

什么文件?说具体点

com.io.hw.file.util.FileUtils;
com.String.widget.util.ValueWidget
com.time.util.TimeHWUtil
这三个文件
5 楼 hw1287789687 2014-08-17  
huangjnjavaIt 写道
楼主,你好,systemUtil.java中引用的其他文件能否提供一下呢?

什么文件?说具体点
4 楼 huangjnjavaIt 2014-08-16  
楼主,你好,systemUtil.java中引用的其他文件能否提供一下呢?
3 楼 hw1287789687 2014-03-13  
foolkeeper 写道
NEGATIVE_ONE 没有定义,是啥值?

NEGATIVE_ONE的值是-1
2 楼 foolkeeper 2014-03-13  
原来在下载里
1 楼 foolkeeper 2014-03-13  
NEGATIVE_ONE 没有定义,是啥值?

相关推荐

Global site tag (gtag.js) - Google Analytics