code来源:https://betheme.net/news/txtlist_i67135v.html?action=onClick
cmd命令行查看文件md5码:certutil -hashfile a.txt md5,不加后面的md5,查看的默认是sha1码。
package com.tools.util; import java.io.File; import java.io.FileInputStream; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.security.MessageDigest; public class MD5Util { private static MappedByteBuffer[] mappedByteBuffers; private static int bufferCount; /** * 获取单个文件的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(); long fileSize = ch.size(); bufferCount = (int) Math.ceil((double) fileSize / (double) Integer.MAX_VALUE); mappedByteBuffers = new MappedByteBuffer[bufferCount]; long preLength = 0; long regionSize = Integer.MAX_VALUE; for (int i = 0; i < bufferCount; i++) { if (fileSize - preLength < Integer.MAX_VALUE) { regionSize = fileSize - preLength; } mappedByteBuffers[i] = ch.map(FileChannel.MapMode.READ_ONLY, preLength, regionSize); preLength += regionSize; } MessageDigest messagedigest = MessageDigest.getInstance("MD5"); for (int i = 0; i < bufferCount; i++) { messagedigest.update(mappedByteBuffers[i]); } 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(); } }
标签:bufferCount,文件,java,int,stringbuffer,cmd,import,md5 From: https://www.cnblogs.com/xsj1989/p/17083007.html