From a47aa433d7a54910351ecca27b44a539653eab35 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期二, 11 十月 2022 16:20:50 +0800
Subject: [PATCH] 1

---
 src/main/java/com/lf/server/service/all/FileService.java |   78 +++++++++++++++++++++++++++++++++++++++
 1 files changed, 78 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/lf/server/service/all/FileService.java b/src/main/java/com/lf/server/service/all/FileService.java
index 47e558e..6e26b23 100644
--- a/src/main/java/com/lf/server/service/all/FileService.java
+++ b/src/main/java/com/lf/server/service/all/FileService.java
@@ -1,17 +1,29 @@
 package com.lf.server.service.all;
 
+import com.lf.server.controller.all.BaseController;
+import com.lf.server.entity.all.ResponseMsg;
+import com.lf.server.entity.sys.AttachEntity;
+import com.lf.server.entity.sys.UserEntity;
+import com.lf.server.helper.PathHelper;
+import com.lf.server.service.sys.AttachService;
+import com.sun.xml.internal.fastinfoset.tools.XML_SAX_StAX_FI;
+import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.tomcat.util.http.fileupload.FileItem;
 import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
 import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
 import org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
 import java.util.List;
 import java.util.UUID;
 
@@ -21,8 +33,74 @@
  */
 @Service
 public class FileService {
+    @Autowired
+    PathHelper pathHelper;
+
+    @Autowired
+    AttachService attachService;
+
+    private static long MAX_FILE_SIZE = 20 * 1024 * 1024;
+
     private final static Log log = LogFactory.getLog(FileService.class);
 
+    public ResponseMsg<String> upload(UserEntity ue, String tab, MultipartFile file, BaseController ctrl) {
+        try {
+            if (file == null && file.isEmpty()) {
+                return ctrl.fail("鏂囦欢涓婁紶涓虹┖", null);
+            }
+            if (file.getSize() > MAX_FILE_SIZE) {
+                return ctrl.fail(String.format("鏂囦欢澶т簬 %d MB", MAX_FILE_SIZE / 1024 / 1024), null);
+            }
+
+            // 浼犺緭鏂囦欢
+            String oldName = file.getOriginalFilename();
+            String filePath = pathHelper.getConfig().getTempPath() + File.separator + oldName;
+            File newFile = new File(filePath);
+            file.transferTo(newFile);
+
+            // 鑾峰彇MD5
+            String md5 = getFileMd5(filePath);
+            AttachEntity entity = attachService.selectByGuid(md5);
+            if (entity != null) {
+                newFile.delete();
+                return ctrl.success("鏂囦欢宸插瓨鍦�", md5);
+            }
+
+            String uploadPath = pathHelper.getUploadFullPath();
+            String targetPath = uploadPath + File.separator + md5;
+            newFile.renameTo(new File(targetPath));
+
+            AttachEntity ae = getAttachEntity(ue, tab, oldName, md5);
+            int rows = attachService.insert(ae);
+
+            return rows > 0 ? ctrl.success(md5) : ctrl.fail("淇濆瓨鏂囦欢澶辫触", null);
+        } catch (Exception ex) {
+            return ctrl.fail(ex.getMessage(), null);
+        }
+    }
+
+    private String getFileMd5(String filePath) throws IOException {
+        FileInputStream fileStream = new FileInputStream(filePath);
+        String md5 = DigestUtils.md5Hex(fileStream);
+        fileStream.close();
+
+        return md5;
+    }
+
+    protected AttachEntity getAttachEntity(UserEntity ue, String tab, String oldName, String md5) {
+        AttachEntity entity = new AttachEntity();
+        entity.setName(oldName);
+        entity.setTab(tab);
+        entity.setGuid(md5);
+        String subPath = PathHelper.getUploadPath() + File.separator + md5;
+        entity.setPath(subPath);
+        if (ue != null) {
+            entity.setCreateUser(ue.getId());
+        }
+
+        return entity;
+    }
+
     /**
      * 涓婁紶鏂囦欢
      *

--
Gitblit v1.9.3