android中获取文件的md5值时首位出现0被省略,而造成了只有31位字符的解决办法。

问题:使用BigInteger 会出现首位为0时MD5值出现31位的现象。

获取单个文件MD5  其中一种操作方式

	/**
	 * 获取单个文件的MD5值!

	 * @param file
	 * @return
	 */

	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);
	}

获取单个文件MD5  另一种操作方式

public String getMd5(File file){
        String value = null;
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            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 (FileNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return value;
}

上面的两种方法都是使用了BigInteger 来保存后,再转化成字符串,都会出现首位为0被忽略的现象。

下面的方法可以解决上述的问题

	/**
	 * 获取单个文件的MD5值!

	 * @param file
	 * @return
	 * 解决首位0被省略问题
	 */

	public static String getFileMD5(File file) {

		StringBuffer stringbuffer = null;
		try {
			char[] hexDigits = { '0', '1', '2','3', '4','5', '6','7','8', '9', 'a','b' ,'c', 'd','e', 'f' };
			FileInputStream in = new FileInputStream(file);
			FileChannel ch = in.getChannel();
			MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0,file.length());
			MessageDigest messagedigest = MessageDigest.getInstance("MD5");
			messagedigest.update(byteBuffer);
			byte[] bytes = messagedigest.digest();
			int n = bytes.length;
			stringbuffer = new StringBuffer(2 * n);
			for (int l = 0; l < n; l++) {
				byte bt = bytes[l];
				char c0 = hexDigits[(bt & 0xf0) >> 4];
				char c1 = hexDigits[bt & 0xf];
				stringbuffer.append(c0);
				stringbuffer.append(c1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return stringbuffer.toString();

	}

但是上面这种方式,如果文件超过2G,会超过 FileChannel 的 map 方法中 size 参数大小限制,源码中发现该参数值大于 Integer.MAX_VALUE 时会直接抛出 IllegalArgumentException(“Size exceeds Integer.MAX_VALUE”) 异常,所以对于特别大的文件其依然不适合。

最终写法参照这篇文章:

Android中获取文件的md5,解决首位0被省略问题,解决超大文件问题

https://blog.csdn.net/dodod2012/article/details/107638940

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值