[Java] 获取文件md5值 →→→→→进入此内容的聊天室

来自 , 2020-07-15, 写在 Java, 查看 114 次.
URL http://www.code666.cn/view/05f971b5
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.nio.MappedByteBuffer;
  5. import java.nio.channels.FileChannel;
  6. import java.security.MessageDigest;
  7. import java.security.NoSuchAlgorithmException;
  8.  
  9. /**
  10.  * 获取文件md5值
  11.  *
  12.  */
  13. public class FileMd5Utils {
  14.         protected static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6',
  15.                         '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  16.         protected static MessageDigest messageDigest = null;
  17.         static {
  18.                 try {
  19.                         messageDigest = MessageDigest.getInstance("MD5");
  20.                 } catch (NoSuchAlgorithmException e) {
  21.                         e.printStackTrace();
  22.                 }
  23.         }
  24.  
  25.         /**
  26.          * 根据文件路径计算文件的MD5
  27.          *
  28.          * @param fileName
  29.          * @return
  30.          * @throws IOException
  31.          */
  32.         public static String getFileMD5String(String fileName) throws IOException {
  33.                 File f = new File(fileName);
  34.                 return getFileMD5String(f);
  35.         }
  36.  
  37.         /**
  38.          * 根据文件对象计算文件的MD5
  39.          *
  40.          * @param file
  41.          * @return
  42.          * @throws IOException
  43.          */
  44.         public static String getFileMD5String(File file) throws IOException {
  45.                 FileInputStream in = new FileInputStream(file);
  46.                 FileChannel ch = in.getChannel();
  47.                 MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0,
  48.                                 file.length());
  49.                 messageDigest.update(byteBuffer);
  50.                 return bufferToHex(messageDigest.digest());
  51.         }
  52.  
  53.         private static String bufferToHex(byte bytes[]) {
  54.                 return bufferToHex(bytes, 0, bytes.length);
  55.         }
  56.  
  57.         private static String bufferToHex(byte bytes[], int m, int n) {
  58.                 StringBuffer stringbuffer = new StringBuffer(2 * n);
  59.                 int k = m + n;
  60.                 for (int l = m; l < k; l++) {
  61.                         appendHexPair(bytes[l], stringbuffer);
  62.                 }
  63.                 return stringbuffer.toString();
  64.         }
  65.  
  66.         private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
  67.                 char c0 = hexDigits[(bt & 0xf0) >> 4];
  68.                 char c1 = hexDigits[bt & 0xf];
  69.                 stringbuffer.append(c0);
  70.                 stringbuffer.append(c1);
  71.         }
  72.  
  73.         /**
  74.          * 测试方法
  75.          *
  76.          * @param args
  77.          * @throws IOException
  78.          */
  79.         public static void main(String[] args) throws IOException {
  80.                 String fileName = "C:\\test.txt"; // 更改文件名不会导致文件md5值变化
  81.                 System.out.println(getFileMD5String(fileName));
  82.         }
  83. }

回复 "获取文件md5值"

这儿你可以回复上面这条便签

captcha