浏览代码

修改多文件上传工具类

hyx 2 年之前
父节点
当前提交
10dabe1784
共有 1 个文件被更改,包括 41 次插入34 次删除
  1. 41 34
      nngkxxdp/src/main/java/com/example/nngkxxdp/util/FileUtil.java

+ 41 - 34
nngkxxdp/src/main/java/com/example/nngkxxdp/util/FileUtil.java

@@ -39,49 +39,47 @@ import sun.net.www.protocol.file.FileURLConnection;
  */
 public class FileUtil {
 
+    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
+
     /**
-     * @param multipartFile
-     * @param fileDir
-     * @return
-     * @throws Exception
-     * @Title: fileUpload
-     * @Description: 上传文件--多文件
-     * @author: zcf
-     * @date: 2020-11-16 17:52:56
-     * @returnType List<String>
+     * @param multipartFiles 文件数组
+     * @param fileDir        上传到的路径
+     * @return 文件上传之后, 保存到硬盘中文件的名称集合
+     * @author: zhoupeng
+     * @date: 2022-01-29 17:06:01
      */
-    public static List<String> fileUpload(MultipartFile[] multipartFile, String fileDir) throws Exception {
-        List<String> result = new ArrayList<>();
-        // 判断多文件上传
-//        if (multipartFile != null && multipartFile.length > 0) {
-//            for (int i = 0; i < multipartFile.length; i++) {
-//                //获取上传过来的文件名
-//                File temporaryFile = new File(fileDir + multipartFile[i].getOriginalFilename());
-//                multipartFile[i].transferTo(temporaryFile);
-//                String type = FileTypeUtil.getType(temporaryFile);
-//                String newName = (new Date()).getTime() + ((int) (Math.random() * 9000) + 1000) + "." + type;
-//                String filePath = fileDir + newName;
-//                File newFile = new File(filePath);
-//                temporaryFile.renameTo(newFile);
-//                // 返回结果
-//                result.add(newName);
-//            }
-//        }
-        if (multipartFile != null) {
-            for (int i = 0; i < multipartFile.length; i++) {
+    public static List<String> fileUpload(MultipartFile[] multipartFiles, String fileDir) throws IOException {
+        List<String> stringList = new ArrayList<>();
+        File folder = new File(fileDir);
+        //是否为目录
+        if (!folder.isDirectory()) {
+            //不是目录
+            folder.mkdirs();
+        }
+        if (Blank.isNotEmpty(multipartFiles) && multipartFiles.length > 0) {
+            for (int i = 0; i < multipartFiles.length; i++) {
                 //获取上传过来的文件名
-                File temporaryFile = new File(fileDir + multipartFile[i].getOriginalFilename());
-                multipartFile[i].transferTo(temporaryFile);
+                String originalFileName = multipartFiles[i].getOriginalFilename();
+                if (Blank.isEmpty(originalFileName)) {
+                    continue;
+                }
+                File temporaryFile = new File(fileDir + "\\" + originalFileName);
+                //保存文件
+                multipartFiles[i].transferTo(temporaryFile);
+//                stringList.add(originalFileName);
+
+                //如果需要更改文件的名称,使用其他命名方式,则执行下面的代码
                 String type = FileTypeUtil.getType(temporaryFile);
-                String newName = (new Date()).getTime() + ((int) (Math.random() * 9000) + 1000) + "." + type;
-                String filePath = fileDir + newName;
+                String newName = generTrfn() + "." + type;
+                String filePath = fileDir + "\\" + newName;
                 File newFile = new File(filePath);
+                //修改文件名
                 temporaryFile.renameTo(newFile);
                 // 返回结果
-                result.add("" + newName);
+                stringList.add(newName);
             }
         }
-        return result;
+        return stringList;
     }
 
     public static Map<String, Object> fileUploads(MultipartFile mFile, String fileDir) throws Exception {
@@ -549,4 +547,13 @@ public class FileUtil {
 //		loadRecourseFromJar();
 //	}
 
+    /**
+     * 返回时间(年月日时分秒毫秒)+4位随机数
+     *
+     * @return
+     */
+    public static String generTrfn() {
+        return simpleDateFormat.format(new Date()) + ((int) (Math.random() * 9000) + 1000);
+    }
+
 }