From ed8c7a5effd0d423ce1118b680ecdca6fe732609 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期三, 02 七月 2025 16:43:13 +0800 Subject: [PATCH] Merge branch 'master' of http://192.168.11.205:9000/r/P2022036_Service --- src/main/java/com/lf/server/service/data/DataQueryService.java | 77 +++++++++++++++++++++++++++++++++++++- 1 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/lf/server/service/data/DataQueryService.java b/src/main/java/com/lf/server/service/data/DataQueryService.java index fa9fa56..283a1de 100644 --- a/src/main/java/com/lf/server/service/data/DataQueryService.java +++ b/src/main/java/com/lf/server/service/data/DataQueryService.java @@ -1,9 +1,20 @@ package com.lf.server.service.data; +import com.lf.server.entity.all.SettingData; +import com.lf.server.entity.sys.AttachEntity; +import com.lf.server.entity.sys.UserEntity; +import com.lf.server.helper.FileHelper; +import com.lf.server.helper.PathHelper; +import com.lf.server.service.sys.AttachService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; /** * 鏁版嵁妫�绱㈡湇鍔� @@ -11,9 +22,71 @@ */ @Service public class DataQueryService { - public Integer uploadFiles(MultipartFile[] files, HttpServletRequest req) { + @Autowired + PathHelper pathHelper; + @Autowired + AttachService attachService; - return 0; + /** + * 涓婁紶鏂囦欢 + */ + public Integer uploadFiles(UserEntity ue, String tabName, String eventid, MultipartFile[] files, HttpServletRequest req) throws IOException { + List<AttachEntity> list = new ArrayList<AttachEntity>(); + for (MultipartFile file : files) { + if (file.getSize() > SettingData.MAX_FILE_SIZE) { + continue; + } + + // 浼犺緭鏂囦欢 + String fileName = file.getOriginalFilename(); + String filePath = pathHelper.getTempPath() + File.separator + fileName; + File newFile = new File(filePath); + file.transferTo(newFile); + double sizes = FileHelper.sizeToMb(file.getSize()); + + // 鑾峰彇MD5 + String md5 = FileHelper.getFileMd5(filePath); + AttachEntity entity = attachService.selectByGuid(md5); + + AttachEntity ae = getAttachEntity(ue, tabName, eventid, fileName, md5, sizes); + if (entity != null) { + ae.setPath(entity.getPath()); + + String oldPath = pathHelper.getConfig().getUploadPath() + File.separator + ae.getPath(); + File f = new File(oldPath); + if (!f.exists() || f.isDirectory()) { + newFile.renameTo(new File(oldPath)); + } else { + newFile.delete(); + } + } else { + String targetPath = pathHelper.getConfig().getUploadPath() + File.separator + ae.getPath(); + newFile.renameTo(new File(targetPath)); + } + + list.add(ae); + } + + return attachService.inserts(list); + } + + /** + * 鑾峰彇闄勪欢瀹炰綋绫� + */ + protected AttachEntity getAttachEntity(UserEntity ue, String tabName, String eventid, String fileName, String md5, double sizes) { + AttachEntity entity = new AttachEntity(); + entity.setTab(tabName); + entity.setTabGuid(eventid); + entity.setName(fileName); + entity.setGuid(md5); + String subPath = PathHelper.getUploadPath() + File.separator + md5 + FileHelper.getExtension(fileName); + entity.setPath(subPath); + entity.setSizes(sizes); + if (ue != null) { + entity.setCreateUser(ue.getId()); + } + + return entity; } } -- Gitblit v1.9.3