From 2f55cebbad3dea187a5f91d16ec80a9677dab699 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期三, 13 十一月 2024 11:16:53 +0800 Subject: [PATCH] 1 --- src/main/java/com/yssh/utils/FileUtils.java | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 59 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/yssh/utils/FileUtils.java b/src/main/java/com/yssh/utils/FileUtils.java new file mode 100644 index 0000000..9df4c55 --- /dev/null +++ b/src/main/java/com/yssh/utils/FileUtils.java @@ -0,0 +1,59 @@ +package com.yssh.utils; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.security.MessageDigest; + +public class FileUtils { + public final static int SIXTEEN = 16; + + public static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + + @SuppressWarnings("unused") + public static String getFileMd5(String filePath) { + FileInputStream fis = null; + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + + fis = new FileInputStream(new File(filePath)); + FileChannel fChannel = fis.getChannel(); + ByteBuffer buffer = ByteBuffer.allocateDirect(1024 * 1024); + + while (fChannel.read(buffer) != -1) { + buffer.flip(); + md.update(buffer); + buffer.compact(); + } + byte[] b = md.digest(); + + return byteToHexString(b); + } catch (Exception ex) { + ex.printStackTrace(); + return null; + } finally { + try { + if (null != fis) { + fis.close(); + } + } catch (IOException ex) { + ex.printStackTrace(); + } + } + } + + public static String byteToHexString(byte[] tmp) { + char[] str = new char[16 * 2]; + + int k = 0; + for (int i = 0; i < SIXTEEN; i++) { + byte byte0 = tmp[i]; + str[k++] = HEX_DIGITS[byte0 >>> 4 & 0xf]; + str[k++] = HEX_DIGITS[byte0 & 0xf]; + } + + return new String(str); + } +} -- Gitblit v1.9.3