maven
3 天以前 3c94476f51df2b90787f9c7c0762a18e3094b80d
src/main/java/com/ruoyi/other/service/impl/TempFileServiceImpl.java
@@ -13,6 +13,7 @@
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -32,10 +33,15 @@
    // 上传到临时目录
    @Override
    public TempFile uploadFile(MultipartFile file) throws IOException {
    public TempFile uploadFile(MultipartFile file,Integer type) throws IOException {
        // 1. 生成临时文件ID和路径
        String tempId = UUID.randomUUID().toString();
        Path tempFilePath = Paths.get(tempDir, tempId + "_" + file.getOriginalFilename());
        String originalFilename = file.getOriginalFilename();
        if(originalFilename == null) throw new IOException("文件名不能为空");
        String encodedFilename = java.net.URLEncoder.encode(originalFilename, StandardCharsets.UTF_8);
        Path tempFilePath = Paths.get(tempDir, tempId + "_" + encodedFilename);
//        Path tempFilePath = Paths.get(tempDir, tempId + "_" + file.getOriginalFilename());
        // 2. 确保目录存在
        Path parentDir = tempFilePath.getParent();
@@ -52,6 +58,7 @@
        tempFileRecord.setOriginalName(file.getOriginalFilename());
        tempFileRecord.setTempPath(tempFilePath.toString());
        tempFileRecord.setExpireTime(LocalDateTime.now().plusHours(2)); // 2小时后过期
        tempFileRecord.setType(type);
        tempFileMapper.insert(tempFileRecord);
        return tempFileRecord;