文件系统上传文件时对文件重命名

master
382696293@qq.com 2 years ago
parent df116c3c2f
commit a96e79bfbc

@ -66,6 +66,11 @@
<artifactId>flossom-common-swagger</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>2.0.8</version>
</dependency>
</dependencies>
<build>

@ -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<SysFile> upload(MultipartFile file)
{
try
{
public R<SysFile> 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<SysFile> upload(MultipartFile file, int index, String condition)
{
try
{
public R<SysFile> 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()));
}
}

Loading…
Cancel
Save