From 9e2845342151a3775219a9092c0d83191fad94d5 Mon Sep 17 00:00:00 2001
From: huangzhen <867217663@qq.com>
Date: 星期三, 13 九月 2023 14:59:47 +0800
Subject: [PATCH] 批量上传文件方法重载
---
ruoyi-file/src/main/java/com/ruoyi/file/service/impl/AttachmentServiceImpl.java | 82 +++++++++++++++++++++++++++++++++++++++++
ruoyi-file/src/main/java/com/ruoyi/file/service/AttachmentService.java | 2 +
2 files changed, 84 insertions(+), 0 deletions(-)
diff --git a/ruoyi-file/src/main/java/com/ruoyi/file/service/AttachmentService.java b/ruoyi-file/src/main/java/com/ruoyi/file/service/AttachmentService.java
index 239cc7e..363b6a5 100644
--- a/ruoyi-file/src/main/java/com/ruoyi/file/service/AttachmentService.java
+++ b/ruoyi-file/src/main/java/com/ruoyi/file/service/AttachmentService.java
@@ -26,6 +26,8 @@
Object saveBatchFileToPath(MultipartFile[] file, String module);
+ Object saveBatchFileToPath(MultipartFile[] file, String module,Long businessId);
+
void downloadForStream(HttpServletResponse response, String key);
void downloadById(HttpServletResponse response, HttpServletRequest request, Long id);
diff --git a/ruoyi-file/src/main/java/com/ruoyi/file/service/impl/AttachmentServiceImpl.java b/ruoyi-file/src/main/java/com/ruoyi/file/service/impl/AttachmentServiceImpl.java
index e5666a0..1272e43 100644
--- a/ruoyi-file/src/main/java/com/ruoyi/file/service/impl/AttachmentServiceImpl.java
+++ b/ruoyi-file/src/main/java/com/ruoyi/file/service/impl/AttachmentServiceImpl.java
@@ -263,6 +263,88 @@
}
@Override
+ public Object saveBatchFileToPath(MultipartFile[] fileList, String module,Long businessId) {
+ //获取对应模块路径
+ String path;
+ //获取对应模块根路径
+ String dcPath;
+ path = filePathProperties.getModule().get(module);
+ dcPath = filePathProperties.getDcPath();
+ List<AttachmentInfo> attachmentInfoList = new ArrayList<>();
+ for (MultipartFile file : fileList) {
+ String originalFilename = file.getOriginalFilename();
+ if (StringUtils.isEmpty(originalFilename)) {
+ logger.error(ERROR_FORMATTER,module, "文件不存在");
+ throw new ServiceException("文件不存在");
+ }
+ if (StringUtils.isEmpty(path)) {
+ logger.error(ERROR_FORMATTER, module, "文件路径不存在");
+ throw new ServiceException("文件路径不存在");
+ }
+ assert originalFilename != null;
+ LocalDateTime now = LocalDateTime.now();
+ File newFile = null;
+ try {
+ //文件标识 UUID 如4d6873609b144945935ae84442711fd6
+ String key = "";
+ String suffix = "";
+ if (originalFilename.contains(".mp3")) {
+ key = originalFilename;
+ suffix = "";
+ } else {
+ key = UUID.randomUUID().toString().replace("-", "");
+ //文件后缀 包含.
+ suffix = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
+ }
+
+ path = path.replace("/", File.separator);
+ //文件模块路径 如 2021/base/build/0421
+ String modulePath = now.getYear() + path + now.format(DateTimeFormatter.ofPattern("MMdd"));
+ //文件路径 如 2021/base/build/0421/4d6873609b144945935ae84442711fd6.后缀
+ String newFilePath = modulePath + File.separator + key + suffix;
+ //文件绝对路径 如 /home/img/2021/base/build/0421/4d6873609b144945935ae84442711fd6.后缀
+ String localPath = dcPath + newFilePath;
+ //文件访问路径 如 /upload/2021/base/build/0421/4d6873609b144945935ae84442711fd6.后缀
+ String url = filePathProperties.getUrlRootPath() + newFilePath.replace(File.separator, "/");
+ newFile = new File(localPath);
+ if (!newFile.exists() && !newFile.mkdirs()) {
+ logger.error(ERROR_FORMATTER, newFilePath, "文件上传失败");
+ throw new ServiceException("文件上传失败");
+ }
+ //上传文件
+ file.transferTo(newFile);
+ //创建文件信息
+ AttachmentInfo attachmentInfo = new AttachmentInfo();
+ attachmentInfo.setDelFlag(0);
+ attachmentInfo.setFileKey(key);
+ attachmentInfo.setFileSuffix(suffix);
+ attachmentInfo.setFilePath(localPath);
+ attachmentInfo.setFileUrl(url);
+ attachmentInfo.setBusinessId(businessId);
+ attachmentInfo.setFileName(file.getOriginalFilename());
+ attachmentInfo.setFileSize(file.getSize());
+ attachmentInfo.setModule(module);
+ attachmentInfo.setFileType(getType(suffix));
+ /* attachmentInfo.setCreateBy(SecurityUtils.getUsername());
+ attachmentInfo.setUpdateBy(SecurityUtils.getUsername());*/
+ attachmentInfo.setUpdateTime(new Date());
+ attachmentInfo.setCreateTime(new Date());
+ attachmentInfoList.add(attachmentInfo);
+
+ } catch (IOException e) {
+ if (newFile != null && newFile.exists()) {
+ newFile.delete();
+ }
+ logger.error(ERROR_FORMATTER, "文件上传失败", e.getMessage());
+ throw new ServiceException("文件上传失败");
+ }
+ }
+ List<AttachmentInfo> detailList = attachmentInfoService.saveBatchAttachment(attachmentInfoList);
+
+ return detailList;
+ }
+
+ @Override
public void downloadForStream(HttpServletResponse response, String key) {
AttachmentInfo byKey = findByKey(key);
File file;
--
Gitblit v1.9.2