From ed8c7a5effd0d423ce1118b680ecdca6fe732609 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期三, 02 七月 2025 16:43:13 +0800
Subject: [PATCH] Merge branch 'master' of http://192.168.11.205:9000/r/P2022036_Service

---
 src/main/java/com/lf/server/helper/FileHelper.java |  349 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 336 insertions(+), 13 deletions(-)

diff --git a/src/main/java/com/lf/server/helper/FileHelper.java b/src/main/java/com/lf/server/helper/FileHelper.java
index 91a82d3..c9a5294 100644
--- a/src/main/java/com/lf/server/helper/FileHelper.java
+++ b/src/main/java/com/lf/server/helper/FileHelper.java
@@ -1,14 +1,51 @@
 package com.lf.server.helper;
 
-import java.io.File;
-import java.util.Date;
+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;
+
+import java.io.*;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.charset.StandardCharsets;
+import java.security.MessageDigest;
+import java.text.DecimalFormat;
+import java.util.List;
 
 /**
  * 鏂囦欢甯姪绫�
  * @author WWW
  */
 public class FileHelper {
-    private final static String POINT = ".";
+    private final static Log log = LogFactory.getLog(FileHelper.class);
+
+    /**
+     * 鑾峰彇鏂囦欢鍚�
+     */
+    public static String getFileName(String file) {
+        int idx = file.lastIndexOf(File.separator);
+        if (idx > -1) {
+            return file.substring(idx + 1);
+        }
+
+        return "";
+    }
+
+    /**
+     * 鑾峰彇鏂囦欢鍚嶇О
+     */
+    public static String getName(String file) {
+        String fileName = getFileName(file);
+        int idx = fileName.lastIndexOf(".");
+        if (idx > -1) {
+            return fileName.substring(0, idx);
+        }
+
+        return fileName;
+    }
 
     /**
      * 鑾峰彇鏂囦欢鎵╁睍鍚�
@@ -20,7 +57,7 @@
 
         String fileName = file.getName().toLowerCase();
 
-        int idx = fileName.lastIndexOf(POINT);
+        int idx = fileName.indexOf(StaticData.POINT);
         if (idx == -1) {
             return "";
         }
@@ -36,7 +73,7 @@
             return "";
         }
 
-        int idx = fileName.lastIndexOf(POINT);
+        int idx = fileName.lastIndexOf(StaticData.POINT);
         if (idx == -1) {
             return "";
         }
@@ -46,9 +83,6 @@
 
     /**
      * 鑾峰彇澶氱敤閫斾簰鑱旂綉閭欢鎵╁睍绫诲瀷
-     *
-     * @param ext 鏂囦欢鎵╁睍鍚�
-     * @return
      */
     public static String getMime(String ext) {
         switch (ext) {
@@ -135,11 +169,300 @@
     }
 
     /**
-     * 鑾峰彇涓存椂璺緞
-     *
-     * @return
+     * 瀛楄妭鍗曚綅鎹㈢畻
      */
-    public static String getTempPath() {
-        return StringHelper.YMD_HM_FORMAT.format(new Date());
+    public static String formatByte(long byteNumber) {
+        double kbNumber = byteNumber / StaticData.D1024;
+        if (kbNumber < StaticData.D1024) {
+            return new DecimalFormat("#.##KB").format(kbNumber);
+        }
+        double mbNumber = kbNumber / StaticData.D1024;
+        if (mbNumber < StaticData.D1024) {
+            return new DecimalFormat("#.##MB").format(mbNumber);
+        }
+        double gbNumber = mbNumber / StaticData.D1024;
+        if (gbNumber < StaticData.D1024) {
+            return new DecimalFormat("#.##GB").format(gbNumber);
+        }
+        double tbNumber = gbNumber / StaticData.D1024;
+
+        return new DecimalFormat("#.##TB").format(tbNumber);
+    }
+
+    /**
+     * 鑾峰彇鏂囦欢澶у皬
+     */
+    public static String getSizes(double mbNumber) {
+        if (mbNumber < StaticData.D1024) {
+            return new DecimalFormat("#.##MB").format(mbNumber);
+        }
+
+        double gbNumber = mbNumber / StaticData.D1024;
+        if (gbNumber < StaticData.D1024) {
+            return new DecimalFormat("#.##GB").format(gbNumber);
+        }
+
+        double tbNumber = gbNumber / StaticData.D1024;
+
+        return new DecimalFormat("#.##TB").format(tbNumber);
+    }
+
+    /**
+     * 鑾峰彇骞虫柟绫�
+     */
+    public static String getSquareMeter(double num) {
+        if (num < StaticData.I1000000) {
+            return new DecimalFormat("#.##骞虫柟绫�").format(num);
+        }
+
+        double knum = num / StaticData.I1000000;
+
+        return new DecimalFormat("#.##骞虫柟鍗冪背").format(knum);
+    }
+
+    /**
+     * byte杞琈B
+     */
+    public static double sizeToMb(long size) {
+        if (size < StaticData.D1050) {
+            return 0.001;
+        }
+
+        String str = String.format("%.3f", size / StaticData.D1024 / StaticData.D1024);
+
+        return Double.parseDouble(str);
+    }
+
+    /**
+     * 3.鑾峰彇鏂囦欢MD5鐮侊紙JDK锛�
+     */
+    public static String getMd5ByJdk(String filePath) throws IOException {
+        FileInputStream fileStream = new FileInputStream(filePath);
+        String md5 = DigestUtils.md5Hex(fileStream);
+        fileStream.close();
+
+        return md5;
+    }
+
+    /**
+     * 2.鑾峰彇蹇�� 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();
+    }
+
+    /**
+     * 鍒犻櫎鏂囦欢
+     */
+    public static void deleteFiles(List<MetaFileEntity> list) {
+        try {
+            for (MetaFileEntity mf : list) {
+                File f = new File(mf.getPath());
+                if (f.exists()) {
+                    f.delete();
+                }
+            }
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+        }
+    }
+
+    /**
+     * 鍒犻櫎鏂囦欢澶�
+     *
+     * @param dir 鏂囦欢澶�
+     */
+    public static void deleteDir(String dir) {
+        File file = new File(dir);
+
+        deleteFiles(file);
+    }
+
+    /**
+     * 绾ц仈鍒犻櫎鏂囦欢
+     *
+     * @param file 鏂囦欢
+     */
+    public static void deleteFiles(File file) {
+        if (null == file || !file.exists()) {
+            return;
+        }
+
+        if (file.isDirectory()) {
+            File[] files = file.listFiles();
+            if (null != files && files.length > 0) {
+                for (File f : files) {
+                    if (f.isDirectory()) {
+                        deleteFiles(f);
+                    } else {
+                        f.delete();
+                    }
+                }
+            }
+        }
+
+        file.delete();
+    }
+
+    /**
+     * 鑾峰彇鐩稿璺緞
+     *
+     * @param file 鏂囦欢
+     * @return 鐩稿璺緞
+     */
+    public static String getRelativePath(String file) {
+        if (StringHelper.isEmpty(file)) {
+            return null;
+        }
+
+        int idx = file.lastIndexOf(File.separator);
+        int start = file.lastIndexOf(File.separator, idx - 1);
+
+        return file.substring(start + 1);
+    }
+
+    /**
+     * 鑾峰彇璺緞
+     *
+     * @param file 鏂囦欢
+     * @return 鏂囦欢璺緞
+     */
+    public static String getPath(String file) {
+        if (StringHelper.isEmpty(file)) {
+            return null;
+        }
+
+        int end = file.lastIndexOf(File.separator);
+
+        return file.substring(0, end);
+    }
+
+    /**
+     * 1.鑾峰彇鏂囦欢鐨凪D5
+     */
+    @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();
+            }
+        }
+    }
+
+    /**
+     * 瀛楄妭鐮佽浆16杩涘埗
+     */
+    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);
+    }
+
+    /**
+     * 鑾峰彇瀛楃涓茬殑MD5鐮�
+     */
+    public static String getStringMd5(String text) {
+        StringBuilder builder = new StringBuilder();
+        try {
+            MessageDigest md5 = MessageDigest.getInstance("MD5");
+
+            byte[] bytes = md5.digest(text.getBytes(StandardCharsets.UTF_8));
+            for (byte aByte : bytes) {
+                builder.append(Integer.toHexString((0x000000FF & aByte) | 0xFFFFFF00).substring(6));
+            }
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+        }
+
+        return builder.toString();
+    }
+
+    /**
+     * 鏍规嵁璺緞鑾峰彇鏂囦欢
+     */
+    public static void getFilesByPath(List<String> list, String path) {
+        File file = new File(path);
+        if (file.isDirectory()) {
+            File[] files = file.listFiles();
+            if (null == files) {
+                return;
+            }
+
+            for (File f : files) {
+                if (f.isDirectory()) {
+                    getFilesByPath(list, f.getPath());
+                } else {
+                    list.add(f.getPath());
+                }
+            }
+        } else {
+            list.add(file.getPath());
+        }
+    }
+
+    /**
+     * 澶嶅埗鏂囦欢
+     *
+     * @param src  婧愭枃浠�
+     * @param dest 鐩綍鏂囦欢
+     */
+    public static void copyFile(File src, File dest) throws IOException {
+        InputStream is = null;
+        OutputStream os = null;
+        try {
+            is = new FileInputStream(src);
+            os = new FileOutputStream(dest);
+
+            byte[] buffer = new byte[1024];
+
+            int length;
+            while ((length = is.read(buffer)) > 0) {
+                os.write(buffer, 0, length);
+            }
+        } finally {
+            os.close();
+            is.close();
+        }
     }
 }

--
Gitblit v1.9.3