| | |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.List; |
| | | import java.util.concurrent.CompletableFuture; |
| | | import java.util.concurrent.ExecutionException; |
| | |
| | | if (file.isEmpty()) { |
| | | return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("文件不能为空"); |
| | | } |
| | | String targetDir = Paths.get(uploadedFolder, "upload").toString(); |
| | | // 获取当前年月日 |
| | | String date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); |
| | | String targetDir = Paths.get(uploadedFolder, date).toString(); |
| | | log.info("目标目录: {}", targetDir); |
| | | createDirectoriesIfNotExists(targetDir); |
| | | try { |
| | | file.transferTo(Paths.get(targetDir, file.getOriginalFilename())); |
| | | // 获取当前时分秒 |
| | | String time = LocalTime.now().format(DateTimeFormatter.ofPattern("HHmmss")); |
| | | // 文件地址全称 |
| | | Path filePath = Paths.get(targetDir, time + "_" + file.getOriginalFilename()); |
| | | // 文件名 |
| | | file.transferTo(filePath); |
| | | return ResponseEntity.ok("文件上传成功"); |
| | | } catch (IOException e) { |
| | | log.error("文件上传失败", e); |