refactor(web): 使用 Path.of 替代 Paths.get
This commit is contained in:
parent
9f53ec4a4b
commit
628cdc9138
@ -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();
|
||||
}
|
||||
|
||||
@ -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) {}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user