From 024e90554d19c2342f27a26f91bbea378f84da82 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期三, 13 十一月 2024 17:25:18 +0800
Subject: [PATCH] 1

---
 src/main/java/com/moon/server/helper/FileHelper.java |  124 ++++++++++------------------------------
 1 files changed, 32 insertions(+), 92 deletions(-)

diff --git a/src/main/java/com/moon/server/helper/FileHelper.java b/src/main/java/com/moon/server/helper/FileHelper.java
index cecd7fe..69e83e5 100644
--- a/src/main/java/com/moon/server/helper/FileHelper.java
+++ b/src/main/java/com/moon/server/helper/FileHelper.java
@@ -6,8 +6,11 @@
 import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 import java.io.*;
+import java.net.URL;
+import java.net.URLDecoder;
 import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
 import java.nio.charset.StandardCharsets;
@@ -15,19 +18,10 @@
 import java.text.DecimalFormat;
 import java.util.List;
 
-/**
- * 鏂囦欢甯姪绫�
- * @author WWW
- */
+@SuppressWarnings("ALL")
 public class FileHelper {
     private final static Log log = LogFactory.getLog(FileHelper.class);
 
-    /**
-     * 鑾峰彇鏂囦欢鍚�
-     *
-     * @param file
-     * @return
-     */
     public static String getFileName(String file) {
         int idx = file.lastIndexOf(File.separator);
         if (idx > -1) {
@@ -37,9 +31,6 @@
         return "";
     }
 
-    /**
-     * 鑾峰彇鏂囦欢鎵╁睍鍚�
-     */
     public static String getExtension(File file) {
         if (file == null) {
             return null;
@@ -55,9 +46,6 @@
         return fileName.substring(idx);
     }
 
-    /**
-     * 鑾峰彇鏂囦欢鎵╁睍鍚�
-     */
     public static String getExtension(String fileName) {
         if (StringHelper.isEmpty(fileName)) {
             return "";
@@ -71,15 +59,8 @@
         return fileName.substring(idx).toLowerCase();
     }
 
-    /**
-     * 鑾峰彇澶氱敤閫斾簰鑱旂綉閭欢鎵╁睍绫诲瀷
-     *
-     * @param ext 鏂囦欢鎵╁睍鍚�
-     * @return
-     */
     public static String getMime(String ext) {
         switch (ext) {
-            // 鍥剧墖
             case ".tif":
             case ".tiff":
                 return "image/tiff";
@@ -92,7 +73,6 @@
                 return "image/jpeg";
             case ".png":
                 return "image/png";
-            // 闊�/瑙嗛
             case ".mp3":
                 return "audio/mp3";
             case ".mp4":
@@ -114,7 +94,6 @@
                 return "application/vnd.rn-realmedia";
             case ".rmvb":
                 return "application/vnd.rn-realmedia-vbr";
-            // 缃戦〉
             case ".js":
                 return "application/x-javascript";
             case ".css":
@@ -131,7 +110,6 @@
             case ".xml":
             case ".svg":
                 return "text/xml";
-            // 鏂囦欢
             case ".txt":
                 return "text/plain";
             case ".dbf":
@@ -155,15 +133,11 @@
                 return "application/x-dwg";
             case ".ext":
                 return "application/x-msdownload";
-            // 榛樿
             default:
                 return "application/octet-stream";
         }
     }
 
-    /**
-     * 瀛楄妭鍗曚綅鎹㈢畻
-     */
     public static String formatByte(long byteNumber) {
         double kbNumber = byteNumber / StaticData.D1024;
         if (kbNumber < StaticData.D1024) {
@@ -182,9 +156,6 @@
         return new DecimalFormat("#.##TB").format(tbNumber);
     }
 
-    /**
-     * 鑾峰彇鏂囦欢澶у皬
-     */
     public static String getSizes(double mbNumber) {
         if (mbNumber < StaticData.D1024) {
             return new DecimalFormat("#.##MB").format(mbNumber);
@@ -200,9 +171,6 @@
         return new DecimalFormat("#.##TB").format(tbNumber);
     }
 
-    /**
-     * byte杞琈B
-     */
     public static double sizeToMb(long size) {
         if (size < StaticData.D1050) {
             return 0.001;
@@ -213,9 +181,6 @@
         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);
@@ -224,9 +189,6 @@
         return md5;
     }
 
-    /**
-     * 2.鑾峰彇蹇�� MD5 鐮�
-     */
     public static String getFastMd5(String filePath) throws IOException {
         String hash = MD5.asHex(MD5.getHash(new File(filePath)));
 
@@ -236,9 +198,6 @@
         return md5.asHex();
     }
 
-    /**
-     * 鍒犻櫎鏂囦欢
-     */
     public static void deleteFiles(List<MetaFileEntity> list) {
         try {
             for (MetaFileEntity mf : list) {
@@ -252,22 +211,12 @@
         }
     }
 
-    /**
-     * 鍒犻櫎鏂囦欢澶�
-     *
-     * @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;
@@ -289,12 +238,6 @@
         file.delete();
     }
 
-    /**
-     * 鑾峰彇鐩稿璺緞
-     *
-     * @param file 鏂囦欢
-     * @return 鐩稿璺緞
-     */
     public static String getRelativePath(String file) {
         if (StringHelper.isEmpty(file)) {
             return null;
@@ -306,12 +249,6 @@
         return file.substring(start + 1);
     }
 
-    /**
-     * 鑾峰彇璺緞
-     *
-     * @param file 鏂囦欢
-     * @return 鏂囦欢璺緞
-     */
     public static String getPath(String file) {
         if (StringHelper.isEmpty(file)) {
             return null;
@@ -322,9 +259,6 @@
         return file.substring(0, end);
     }
 
-    /**
-     * 1.鑾峰彇鏂囦欢鐨凪D5
-     */
     @SuppressWarnings("unused")
     public static String getFileMd5(String filePath) {
         FileInputStream fis = null;
@@ -357,31 +291,18 @@
         }
     }
 
-    /**
-     * 瀛楄妭鐮佽浆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.I16; 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 {
@@ -398,9 +319,6 @@
         return builder.toString();
     }
 
-    /**
-     * 鏍规嵁璺緞鑾峰彇鏂囦欢
-     */
     public static void getFilesByPath(List<String> list, String path) {
         File file = new File(path);
         if (file.isDirectory()) {
@@ -421,12 +339,6 @@
         }
     }
 
-    /**
-     * 澶嶅埗鏂囦欢
-     *
-     * @param src  婧愭枃浠�
-     * @param dest 鐩綍鏂囦欢
-     */
     public static void copyFile(File src, File dest) throws IOException {
         InputStream is = null;
         OutputStream os = null;
@@ -445,4 +357,32 @@
             is.close();
         }
     }
+
+    public static String filter(String str) {
+        return str.replaceAll("/\\*[\\s\\S]*?\\*/", "");
+    }
+
+    public static String decode(String str) {
+        try {
+            return URLDecoder.decode(str, "UTF-8");
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+            return null;
+        }
+    }
+
+    public static String getClassPathForJar() {
+        URL resource = ClassLoader.getSystemResource("");
+        if (null == resource) {
+            return null;
+        }
+
+        return decode(filter(resource.getPath()));
+    }
+
+    public static String getClassPathForWar() throws IOException {
+        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
+
+        return context.getResource("").getFile().getAbsolutePath();
+    }
 }

--
Gitblit v1.9.3