首页 > 其他 > 详细

获取单个文件的MD5值

时间:2015-08-30 17:52:20      阅读:254      评论:0      收藏:0      [点我收藏+]


基础知识:

  1. MessageDigest

  2. FileInputStream

  3. 技巧while((len=in.read(buffer,0,1024))!=-1){

    }

  4. BigInteger


public static String getFileMD5(File file) {
		if (!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)) != -1) {
				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);
	}


































本文出自 “信息安全-数据库安全” 博客,请务必保留此出处http://fergusj.blog.51cto.com/8835051/1689700

获取单个文件的MD5值

原文:http://fergusj.blog.51cto.com/8835051/1689700

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!