From 3bd15ca130fde30d38d63b1e343400ebebd7100e Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期五, 30 十二月 2022 15:34:53 +0800
Subject: [PATCH] 12.30.4

---
 src/main/java/com/lf/server/config/InitConfig.java     |   22 +++++++
 src/main/java/com/lf/server/entity/all/StaticData.java |   40 ++++++++++---
 src/main/java/com/lf/server/helper/FileHelper.java     |  101 ++++++++++++++++++++++++++++-----
 pom.xml                                                |    6 ++
 4 files changed, 142 insertions(+), 27 deletions(-)

diff --git a/pom.xml b/pom.xml
index 0e6f9cb..3c70b5e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -236,6 +236,12 @@
             <version>3.1.0</version>
             <scope>provided</scope>
         </dependency>
+        <!--fast-md5-->
+        <dependency>
+            <groupId>com.joyent.util</groupId>
+            <artifactId>fast-md5</artifactId>
+            <version>2.7.1</version>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/src/main/java/com/lf/server/config/InitConfig.java b/src/main/java/com/lf/server/config/InitConfig.java
index 5041d70..dd81e8e 100644
--- a/src/main/java/com/lf/server/config/InitConfig.java
+++ b/src/main/java/com/lf/server/config/InitConfig.java
@@ -77,6 +77,7 @@
             //testReadExcel();
             //testBatchUpdate();
             //testAccess();
+            //testMd5();
 
             //boolean f1 = ZipHelper.unzip("D:\\LF\\data\\resources.zip", "D:\\LF\\data\\unzip");
             //boolean f2 = ZipHelper.zip("D:\\LF\\data\\res.zip", "D:\\LF\\data\\unzip\\resources");
@@ -227,4 +228,25 @@
         List<Map<String, Object>> list = AccessHelper.readMdb("D:\\LF\\data\\test.mdb", "", "");
         int count = null == list || list.isEmpty() ? 0 : list.size();
     }
+
+    private void testMd5() {
+        try {
+            String file = "E:\\data\\mpt\\A4mpt20131125.mpt";
+
+            long stime = System.currentTimeMillis();
+            //String md51 = FileHelper.getFileMd5(file);
+            String md51 = FileHelper.getFileMd5(new java.io.File(file));
+            long etime = System.currentTimeMillis();
+            String s1 = String.format("鎵ц鏃堕暱锛�%d 姣.", (etime - stime));
+
+            stime = System.currentTimeMillis();
+            String md52 = FileHelper.getFastMd5(file);
+            etime = System.currentTimeMillis();
+            String s2 = String.format("鎵ц鏃堕暱锛�%d 姣.", (etime - stime));
+
+            etime = System.currentTimeMillis();
+        } catch (Exception ex) {
+            //
+        }
+    }
 }
diff --git a/src/main/java/com/lf/server/entity/all/StaticData.java b/src/main/java/com/lf/server/entity/all/StaticData.java
index 0918b23..00e885b 100644
--- a/src/main/java/com/lf/server/entity/all/StaticData.java
+++ b/src/main/java/com/lf/server/entity/all/StaticData.java
@@ -30,9 +30,39 @@
     public final static int TEN = 10;
 
     /**
+     * 鏁板�硷細16
+     */
+    public final static int SIXTEEN = 16;
+
+    /**
      * 鏁板�硷細200
      */
     public final static int TWO_HUNDRED = 200;
+
+    /**
+     * 鏁板�硷細1024.0
+     */
+    public static final double D1024 = 1024.0;
+
+    /**
+     * 鏁板�硷細1050.0
+     */
+    public static final double D1050 = 1050.0;
+
+    /**
+     * 16杩涘埗
+     */
+    public static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
+
+    /**
+     * 瀛楃鐐�
+     */
+    public final static String POINT = ".";
+
+    /**
+     * 绛夊彿
+     */
+    public final static String EQ = "=";
 
     /**
      * 鐗堟湰鍙�
@@ -68,16 +98,6 @@
      * 涓婁紶鏂囦欢绫诲瀷
      */
     public final static String FILE_TYPES = "'xls','shp','gdb','mdb'";
-
-    /**
-     * 瀛楃鐐�
-     */
-    public final static String POINT = ".";
-
-    /**
-     * 绛夊彿
-     */
-    public final static String EQ = "=";
 
     /**
      * Object瀵硅薄
diff --git a/src/main/java/com/lf/server/helper/FileHelper.java b/src/main/java/com/lf/server/helper/FileHelper.java
index 85bfbd4..38e8a7d 100644
--- a/src/main/java/com/lf/server/helper/FileHelper.java
+++ b/src/main/java/com/lf/server/helper/FileHelper.java
@@ -1,6 +1,8 @@
 package com.lf.server.helper;
 
+import com.lf.server.entity.all.StaticData;
 import com.lf.server.entity.data.MetaFileEntity;
+import com.twmacinta.util.MD5;
 import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -8,6 +10,9 @@
 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;
 import java.text.DecimalFormat;
 import java.util.List;
 
@@ -16,12 +21,6 @@
  * @author WWW
  */
 public class FileHelper {
-    private final static String POINT = ".";
-
-    public static final double D1024 = 1024.0;
-
-    public static final double D1050 = 1050.0;
-
     private final static Log log = LogFactory.getLog(FileHelper.class);
 
     /**
@@ -49,7 +48,7 @@
 
         String fileName = file.getName().toLowerCase();
 
-        int idx = fileName.lastIndexOf(POINT);
+        int idx = fileName.lastIndexOf(StaticData.POINT);
         if (idx == -1) {
             return "";
         }
@@ -65,7 +64,7 @@
             return "";
         }
 
-        int idx = fileName.lastIndexOf(POINT);
+        int idx = fileName.lastIndexOf(StaticData.POINT);
         if (idx == -1) {
             return "";
         }
@@ -167,19 +166,19 @@
      * 瀛楄妭鍗曚綅鎹㈢畻
      */
     public static String formatByte(long byteNumber) {
-        double kbNumber = byteNumber / D1024;
-        if (kbNumber < D1024) {
+        double kbNumber = byteNumber / StaticData.D1024;
+        if (kbNumber < StaticData.D1024) {
             return new DecimalFormat("#.##KB").format(kbNumber);
         }
-        double mbNumber = kbNumber / D1024;
-        if (mbNumber < D1024) {
+        double mbNumber = kbNumber / StaticData.D1024;
+        if (mbNumber < StaticData.D1024) {
             return new DecimalFormat("#.##MB").format(mbNumber);
         }
-        double gbNumber = mbNumber / D1024;
-        if (gbNumber < D1024) {
+        double gbNumber = mbNumber / StaticData.D1024;
+        if (gbNumber < StaticData.D1024) {
             return new DecimalFormat("#.##GB").format(gbNumber);
         }
-        double tbNumber = gbNumber / D1024;
+        double tbNumber = gbNumber / StaticData.D1024;
 
         return new DecimalFormat("#.##TB").format(tbNumber);
     }
@@ -188,11 +187,11 @@
      * byte杞琈B
      */
     public static double sizeToMb(long size) {
-        if (size < D1050) {
+        if (size < StaticData.D1050) {
             return 0.001;
         }
 
-        String str = String.format("%.3f", size / D1024 / D1024);
+        String str = String.format("%.3f", size / StaticData.D1024 / StaticData.D1024);
 
         return Double.parseDouble(str);
     }
@@ -206,6 +205,18 @@
         fileStream.close();
 
         return md5;
+    }
+
+    /**
+     * 鑾峰彇蹇�� MD5 鐮�
+     */
+    public static String getFastMd5(String filePath) throws IOException {
+        String hash = MD5.asHex(MD5.getHash(new File(filePath)));
+
+        MD5 md5 = new MD5();
+        md5.Update(hash, null);
+
+        return md5.asHex();
     }
 
     /**
@@ -291,4 +302,60 @@
 
         return file.substring(0, end);
     }
+
+    /**
+     * 鑾峰彇鏂囦欢鐨凪D5
+     * @param file
+     * @return
+     */
+    @SuppressWarnings("unused")
+    public static String getFileMd5(File file) {
+        FileInputStream fis = null;
+        try {
+            MessageDigest md = MessageDigest.getInstance("MD5");
+
+            fis = new FileInputStream(file);
+            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) {
+        // 姣忎釜瀛楄妭鐢� 16 杩涘埗琛ㄧず鐨勮瘽锛屼娇鐢ㄤ袱涓瓧绗︼紝
+        char[] str = new char[16 * 2];
+
+        // 鎵�浠ヨ〃绀烘垚 16 杩涘埗闇�瑕� 32 涓瓧绗︼紝琛ㄧず杞崲缁撴灉涓搴旂殑瀛楃浣嶇疆
+        int k = 0;
+        // 浠庣涓�涓瓧鑺傚紑濮嬶紝瀵� MD5 鐨勬瘡涓�涓瓧鑺�
+        for (int i = 0; i < StaticData.SIXTEEN; i++) {
+            // 杞崲鎴� 16 杩涘埗瀛楃鐨勮浆鎹�
+            byte byte0 = tmp[i];
+            // 鍙栧瓧鑺備腑楂� 4 浣嶇殑鏁板瓧杞崲
+            str[k++] = StaticData.HEX_DIGITS[byte0 >>> 4 & 0xf];
+            // >>> 涓洪�昏緫鍙崇Щ锛屽皢绗﹀彿浣嶄竴璧峰彸绉伙紝 鍙栧瓧鑺備腑浣� 4 浣嶇殑鏁板瓧杞崲
+            str[k++] = StaticData.HEX_DIGITS[byte0 & 0xf];
+        }
+        // 鎹㈠悗鐨勭粨鏋滆浆鎹负瀛楃涓�
+        return new String(str);
+    }
 }

--
Gitblit v1.9.3