| | |
| | | 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; |
| | |
| | | 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(); |