deason il y a 3 ans
Parent
commit
34c8603e13

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

@@ -5,8 +5,11 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.File;
+import java.text.SimpleDateFormat;
 import java.util.Arrays;
 import java.util.Comparator;
+import java.util.Date;
+import java.util.Random;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -91,4 +94,24 @@ public class FileUtil {
         return true;
     }
 
+    /**
+     * 生成日期目录:yyyyMMdd
+     */
+    public static String dateDir() {
+        return new SimpleDateFormat("yyyyMMdd").format(new Date());
+    }
+
+    /**
+     * 生成文件名(15位时间戳+3位随机字母)
+     */
+    public static String generateFileName() {
+        Random r = new Random();
+        StringBuffer xyz = new StringBuffer();
+        for (int i = 0; i < 3; i++) {
+            // 小写字母
+            xyz.append((char) (r.nextInt(26) + 97));
+        }
+        return new SimpleDateFormat("yyMMddHHmmssSSS").format(new Date()) + xyz;
+    }
+
 }