deason 3 vuotta sitten
vanhempi
commit
4731331ae6

+ 24 - 0
examcloud-commons/src/main/java/cn/com/qmth/examcloud/commons/util/FileUtil.java

@@ -1,10 +1,14 @@
 package cn.com.qmth.examcloud.commons.util;
 
 import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.io.File;
 import java.util.Arrays;
 import java.util.Comparator;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * 文件工具
@@ -15,6 +19,8 @@ import java.util.Comparator;
  */
 public class FileUtil {
 
+    private static final Logger log = LoggerFactory.getLogger(FileUtil.class);
+
     /**
      * 排序
      *
@@ -51,4 +57,22 @@ public class FileUtil {
         return "";
     }
 
+    /**
+     * 检查文件后缀名是否有效(忽略大小写)
+     *
+     * @param allowFileSuffix 允许文件后缀名,多个后缀用|分割,示例:jpg|png
+     * @param fileName        当前文件名 或 后缀名,示例:xxx.jpg 或 .jpg
+     * @return
+     */
+    public static boolean checkFileSuffix(String allowFileSuffix, String fileName) {
+        if (StringUtils.isEmpty(allowFileSuffix) || StringUtils.isEmpty(fileName)) {
+            log.warn("allowFileSuffix = {}, fileName = {}", allowFileSuffix, fileName);
+            return false;
+        }
+
+        String regex = String.format("\\.(?:%s)$", allowFileSuffix.toLowerCase());
+        Matcher matcher = Pattern.compile(regex).matcher(fileName.toLowerCase());
+        return matcher.find();
+    }
+
 }