diff --git a/flossom-modules/flossom-file/pom.xml b/flossom-modules/flossom-file/pom.xml index d0fa8eb..7386a3e 100644 --- a/flossom-modules/flossom-file/pom.xml +++ b/flossom-modules/flossom-file/pom.xml @@ -66,6 +66,11 @@ flossom-common-swagger + + org.springframework + spring-mock + 2.0.8 + diff --git a/flossom-modules/flossom-file/src/main/java/com/flossom/file/controller/SysFileController.java b/flossom-modules/flossom-file/src/main/java/com/flossom/file/controller/SysFileController.java index 591157d..b4d5e94 100644 --- a/flossom-modules/flossom-file/src/main/java/com/flossom/file/controller/SysFileController.java +++ b/flossom-modules/flossom-file/src/main/java/com/flossom/file/controller/SysFileController.java @@ -2,16 +2,23 @@ package com.flossom.file.controller; import com.flossom.common.core.domain.SysFile; import com.flossom.file.service.ISysFileService; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mock.web.MockMultipartFile; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import com.flossom.common.core.domain.R; import com.flossom.common.core.utils.file.FileUtils; -import java.net.URLEncoder; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.util.UUID; /** * 文件请求处理 @@ -19,8 +26,7 @@ import java.net.URLEncoder; * @author flossom */ @RestController -public class SysFileController -{ +public class SysFileController { private static final Logger log = LoggerFactory.getLogger(SysFileController.class); @Autowired @@ -30,24 +36,21 @@ public class SysFileController * 文件上传请求 */ @PostMapping("upload") - public R upload(MultipartFile file) - { - try - { + public R upload(MultipartFile file) { + try { // 上传并返回访问地址 + log.info("文件上传前名称:{}", file.getOriginalFilename()); + file = renameFile(file, UUID.randomUUID().toString().replace("-","")); System.out.println("文件上传开始"); String url = sysFileService.uploadFile(file); SysFile sysFile = new SysFile(); String fileName = FileUtils.getName(url); - log.info("文件上传前名称:{}", fileName); - String encodeFileName = URLEncoder.encode(fileName, "utf-8"); - log.info("编码后文件名:{}", encodeFileName); - sysFile.setName(encodeFileName); + log.info("重置文件名:{}", fileName); + sysFile.setName(fileName); sysFile.setUrl(url); + int i = 1/0; return R.ok(sysFile); - } - catch (Exception e) - { + } catch (Exception e) { log.error("上传文件失败", e); return R.fail(e.getMessage()); } @@ -57,28 +60,52 @@ public class SysFileController * 文件上传请求 */ @PostMapping("upload/else") - public R upload(MultipartFile file, int index, String condition) - { - try - { + public R upload(MultipartFile file, int index, String condition) { + try { // 上传并返回访问地址 + log.info("文件上传前名称:{}", file.getOriginalFilename()); + file = renameFile(file, UUID.randomUUID().toString().replace("-","")); System.out.println("文件上传开始"); String url = sysFileService.uploadFile(file); SysFile sysFile = new SysFile(); String fileName = FileUtils.getName(url); - log.info("文件上传前名称:{}", fileName); - String encodeFileName = URLEncoder.encode(fileName, "utf-8"); - log.info("编码后文件名:{}", encodeFileName); - sysFile.setName(encodeFileName); + log.info("重置文件名:{}", fileName); + sysFile.setName(fileName); sysFile.setUrl(url); sysFile.setIndex(index); sysFile.setCondition(condition); return R.ok(sysFile); - } - catch (Exception e) - { + } catch (Exception e) { log.error("上传文件失败", e); return R.fail(e.getMessage()); } } + + + public MultipartFile renameFile(MultipartFile originalFile, String newFileName) throws IOException { + if (originalFile == null || StringUtils.isEmpty(newFileName)) { + throw new IllegalArgumentException("Original file and new file name must not be null or empty."); + } + + // Create a temporary file to store the renamed content + File tempFile = File.createTempFile("temp-", ".tmp"); + Path tempFilePath = tempFile.toPath(); + + // Copy the content of the original MultipartFile to the temporary file + Files.copy(originalFile.getInputStream(), tempFilePath, StandardCopyOption.REPLACE_EXISTING); + + // Generate the new file name + String originalFileName = originalFile.getOriginalFilename(); + String fileExtension = originalFileName.substring(originalFileName.lastIndexOf(".")); + String finalFileName = newFileName + fileExtension; + + // Rename the temporary file + File renamedFile = new File(tempFile.getParent(), finalFileName); + Files.move(tempFilePath, renamedFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + + // Create a new MultipartFile from the renamed file + return new MockMultipartFile(renamedFile.getName(), renamedFile.getName(), + originalFile.getContentType(), Files.readAllBytes(renamedFile.toPath())); + } + }