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/mapper/sys/AttachMapper.java | 8 ++ src/main/java/com/lf/server/controller/all/SignController.java | 2 src/main/java/com/lf/server/controller/data/StyleController.java | 18 ++++ src/main/java/com/lf/server/helper/PathHelper.java | 54 +++++++++---- src/main/java/com/lf/server/service/sys/AttachService.java | 5 + src/main/java/com/lf/server/controller/all/FileController.java | 2 src/main/resources/mapper/sys/AttachMapper.xml | 4 + src/main/java/com/lf/server/controller/sys/ResController.java | 19 ++++ src/main/java/com/lf/server/service/all/FileService.java | 78 +++++++++++++++++++ 9 files changed, 167 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/lf/server/controller/all/FileController.java b/src/main/java/com/lf/server/controller/all/FileController.java index db76f07..ebaae93 100644 --- a/src/main/java/com/lf/server/controller/all/FileController.java +++ b/src/main/java/com/lf/server/controller/all/FileController.java @@ -34,8 +34,6 @@ @Autowired private FileService fileService; - private final static Log log = LogFactory.getLog(FileController.class); - @SysLog() @ApiOperation(value = "鏂囦欢涓婁紶") @PostMapping(value = "/upload") diff --git a/src/main/java/com/lf/server/controller/all/SignController.java b/src/main/java/com/lf/server/controller/all/SignController.java index a284051..60106ae 100644 --- a/src/main/java/com/lf/server/controller/all/SignController.java +++ b/src/main/java/com/lf/server/controller/all/SignController.java @@ -50,8 +50,6 @@ @Autowired PropertiesConfig propertiesConfig; - private static final Log log = LogFactory.getLog(SignController.class); - @SysLog() @ApiOperation(value = "璺宠浆棣栭〉") @GetMapping({"/", "/toIndex"}) diff --git a/src/main/java/com/lf/server/controller/data/StyleController.java b/src/main/java/com/lf/server/controller/data/StyleController.java index 4782e46..d3da816 100644 --- a/src/main/java/com/lf/server/controller/data/StyleController.java +++ b/src/main/java/com/lf/server/controller/data/StyleController.java @@ -5,6 +5,7 @@ import com.lf.server.entity.all.ResponseMsg; import com.lf.server.entity.data.StyleEntity; import com.lf.server.entity.sys.UserEntity; +import com.lf.server.service.all.FileService; import com.lf.server.service.data.StyleService; import com.lf.server.service.sys.TokenService; import io.swagger.annotations.Api; @@ -13,6 +14,7 @@ import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.util.List; @@ -31,6 +33,11 @@ @Autowired TokenService tokenService; + + @Autowired + FileService fileService; + + private final static String TAB_NAME = "sys_style"; @SysLog() @ApiOperation(value = "鏌ヨ璁板綍鏁�") @@ -209,7 +216,6 @@ public ResponseMsg<StyleEntity> selectStyle(int id) { try { StyleEntity styleEntity = styleService.selectStyle(id); - ; return success(styleEntity); } catch (Exception ex) { @@ -229,4 +235,14 @@ return fail(ex.getMessage(), null); } } + + @SysLog() + @ApiOperation(value = "涓婁紶鏂囦欢") + @ResponseBody + @PostMapping(value = "/upload") + public ResponseMsg<String> upload(@RequestBody MultipartFile file, HttpServletRequest req) { + UserEntity ue = tokenService.getCurrentUser(req); + + return fileService.upload(ue, TAB_NAME, file, this); + } } diff --git a/src/main/java/com/lf/server/controller/sys/ResController.java b/src/main/java/com/lf/server/controller/sys/ResController.java index 390657f..293b586 100644 --- a/src/main/java/com/lf/server/controller/sys/ResController.java +++ b/src/main/java/com/lf/server/controller/sys/ResController.java @@ -5,6 +5,7 @@ import com.lf.server.entity.all.ResponseMsg; import com.lf.server.entity.sys.ResEntity; import com.lf.server.entity.sys.UserEntity; +import com.lf.server.service.all.FileService; import com.lf.server.service.sys.ResService; import com.lf.server.service.sys.TokenService; import io.swagger.annotations.Api; @@ -13,6 +14,7 @@ import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.util.List; @@ -25,12 +27,17 @@ @Api(tags = "杩愮淮绠$悊\\璧勬簮绠$悊") @RestController @RequestMapping("/res") -public class ResController extends BaseController { +public class ResController extends BaseController { @Autowired ResService resService; @Autowired TokenService tokenService; + + @Autowired + FileService fileService; + + private final static String TAB_NAME = "sys_res"; @SysLog() @ApiOperation(value = "鏌ヨ璁板綍鏁�") @@ -223,4 +230,14 @@ return fail(ex.getMessage(), null); } } + + @SysLog() + @ApiOperation(value = "涓婁紶鏂囦欢") + @ResponseBody + @PostMapping(value = "/upload") + public ResponseMsg<String> upload(@RequestBody MultipartFile file, HttpServletRequest req) { + UserEntity ue = tokenService.getCurrentUser(req); + + return fileService.upload(ue, TAB_NAME, file, this); + } } diff --git a/src/main/java/com/lf/server/helper/PathHelper.java b/src/main/java/com/lf/server/helper/PathHelper.java index 327240e..be75b94 100644 --- a/src/main/java/com/lf/server/helper/PathHelper.java +++ b/src/main/java/com/lf/server/helper/PathHelper.java @@ -13,7 +13,7 @@ @Component public class PathHelper { @Autowired - private PropertiesConfig propertiesConfig; + private PropertiesConfig config; private static int MAX_FILES = 2000; @@ -25,14 +25,34 @@ private static int sharePath = 1; + public PropertiesConfig getConfig() { + return config; + } + + public static int getImportPath() { + return importPath; + } + + public static int getExportPath() { + return exportPath; + } + + public static int getUploadPath() { + return uploadPath; + } + + public static int getSharePath() { + return sharePath; + } + /** * 鍒濆鍖� */ public void init() { - importPath = getSubPath(propertiesConfig.getImportPath(), importPath); - exportPath = getSubPath(propertiesConfig.getExportPath(), exportPath); - uploadPath = getSubPath(propertiesConfig.getUploadPath(), uploadPath); - sharePath = getSubPath(propertiesConfig.getSharePath(), sharePath); + importPath = getSubPath(config.getImportPath(), importPath); + exportPath = getSubPath(config.getExportPath(), exportPath); + uploadPath = getSubPath(config.getUploadPath(), uploadPath); + sharePath = getSubPath(config.getSharePath(), sharePath); } private static int getSubPath(String parentPath, int subPath) { @@ -59,10 +79,10 @@ * * @return */ - public String getImportPath() { - importPath = getSubPath(propertiesConfig.getImportPath(), importPath); + public String getImportFullPath() { + importPath = getSubPath(config.getImportPath(), importPath); - return propertiesConfig.getImportPath() + File.separator + importPath; + return config.getImportPath() + File.separator + importPath; } /** @@ -70,10 +90,10 @@ * * @return */ - public String getExportPath() { - exportPath = getSubPath(propertiesConfig.getExportPath(), exportPath); + public String getExportFullPath() { + exportPath = getSubPath(config.getExportPath(), exportPath); - return propertiesConfig.getExportPath() + File.separator + exportPath; + return config.getExportPath() + File.separator + exportPath; } /** @@ -81,10 +101,10 @@ * * @return */ - public String getUploadPath() { - uploadPath = getSubPath(propertiesConfig.getUploadPath(), uploadPath); + public String getUploadFullPath() { + uploadPath = getSubPath(config.getUploadPath(), uploadPath); - return propertiesConfig.getUploadPath() + File.separator + uploadPath; + return config.getUploadPath() + File.separator + uploadPath; } /** @@ -92,9 +112,9 @@ * * @return */ - public String getSharePath() { - sharePath = getSubPath(propertiesConfig.getSharePath(), sharePath); + public String getShareFullPath() { + sharePath = getSubPath(config.getSharePath(), sharePath); - return propertiesConfig.getSharePath() + File.separator + sharePath; + return config.getSharePath() + File.separator + sharePath; } } diff --git a/src/main/java/com/lf/server/mapper/sys/AttachMapper.java b/src/main/java/com/lf/server/mapper/sys/AttachMapper.java index 8fa41ff..e1519a9 100644 --- a/src/main/java/com/lf/server/mapper/sys/AttachMapper.java +++ b/src/main/java/com/lf/server/mapper/sys/AttachMapper.java @@ -47,6 +47,14 @@ public AttachEntity selectById(int id); /** + * 鏍规嵁Guid鏌ヨ + * + * @param guid + * @return + */ + public AttachEntity selectByGuid(String guid); + + /** * 鎻掑叆涓�鏉� * * @param entity 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; + } + /** * 涓婁紶鏂囦欢 * diff --git a/src/main/java/com/lf/server/service/sys/AttachService.java b/src/main/java/com/lf/server/service/sys/AttachService.java index cff2e8a..8cb0486 100644 --- a/src/main/java/com/lf/server/service/sys/AttachService.java +++ b/src/main/java/com/lf/server/service/sys/AttachService.java @@ -42,6 +42,11 @@ } @Override + public AttachEntity selectByGuid(String guid) { + return attachMapper.selectByGuid(guid); + } + + @Override public Integer insert(AttachEntity entity) { return attachMapper.insert(entity); } diff --git a/src/main/resources/mapper/sys/AttachMapper.xml b/src/main/resources/mapper/sys/AttachMapper.xml index c89f886..c711e75 100644 --- a/src/main/resources/mapper/sys/AttachMapper.xml +++ b/src/main/resources/mapper/sys/AttachMapper.xml @@ -29,6 +29,10 @@ select * from lf.sys_attach where id = #{id} </select> + <select id="selectByGuid" resultType="com.lf.server.entity.sys.AttachEntity"> + select * from lf.sys_attach where guid = #{guid} + </select> + <insert id="insert" parameterType="com.lf.server.entity.sys.AttachEntity"> insert into lf.sys_attach (name,tab,guid,path,create_user,create_time) -- Gitblit v1.9.3