refactor(web): 使用 Path.of 替代 Paths.get

This commit is contained in:
user 2025-03-14 23:52:25 +08:00
parent 9f53ec4a4b
commit 628cdc9138
2 changed files with 4 additions and 4 deletions

View File

@ -36,7 +36,7 @@ public class FileUploadResource {
public Mono<ResponseEntity<Map<String, String>>> uploadFile(@RequestPart("file") FilePart file) {
return Mono.fromCallable(() -> {
// 确保上传目录存在
Path uploadDir = Paths.get(uploadPath);
Path uploadDir = Path.of(uploadPath);
if (!Files.exists(uploadDir)) {
Files.createDirectories(uploadDir);
}
@ -113,7 +113,7 @@ public class FileUploadResource {
@GetMapping("/files/{filename:.+}")
public Mono<ResponseEntity<byte[]>> getFile(@PathVariable String filename) {
return Mono.fromCallable(() -> {
Path file = Paths.get(uploadPath).resolve(filename);
Path file = Path.of(uploadPath).resolve(filename);
if (!Files.exists(file)) {
return ResponseEntity.notFound().build();
}

View File

@ -277,7 +277,7 @@ public class UserResource {
String fileExtension = getFileExtension(filePart.filename());
String newFileName = UUID.randomUUID().toString() + fileExtension;
String relativePath = "/" + newFileName;
Path uploadPath = Paths.get(avatarDir).toAbsolutePath().normalize();
Path uploadPath = Path.of(avatarDir).toAbsolutePath().normalize();
Path filePath = uploadPath.resolve(newFileName).normalize();
try {
@ -293,7 +293,7 @@ public class UserResource {
// 删除旧头像文件
if (user.getAvatar() != null) {
try {
Path oldAvatarPath = Paths.get(avatarDir, new File(user.getAvatar()).getName());
Path oldAvatarPath = Path.of(avatarDir).resolve(Path.of(user.getAvatar()).getFileName());
Files.deleteIfExists(oldAvatarPath);
} catch (IOException e) {}
}