From 3c04019af4f29deff170ec5c77a74c88e8f90692 Mon Sep 17 00:00:00 2001 From: MaDaLei Date: Mon, 13 Apr 2026 14:29:35 +0800 Subject: [PATCH] feat: make upload path and base-url configurable via properties --- .../petstore/controller/FileController.java | 22 +++++++++---------- .../petstore/controller/ReportController.java | 6 +++-- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/petstore/controller/FileController.java b/src/main/java/com/petstore/controller/FileController.java index ea0f0f5..83e74f3 100644 --- a/src/main/java/com/petstore/controller/FileController.java +++ b/src/main/java/com/petstore/controller/FileController.java @@ -26,38 +26,38 @@ import java.util.UUID; @CrossOrigin public class FileController { - private static final String UPLOAD_BASE = "/Users/wac/Desktop/www/_src/petstore/backend/uploads/"; - - @Value("${upload.path:uploads}") + @Value("${upload.path:uploads/}") private String uploadPath; @GetMapping("/image/**") public ResponseEntity getImage(HttpServletRequest request) throws IOException { String path = request.getRequestURI().replace("/api/upload/image", ""); - File file = new File(UPLOAD_BASE + path); + String basePath = uploadPath.endsWith("/") ? uploadPath : uploadPath + "/"; + File file = new File(basePath + path); if (!file.exists()) { return ResponseEntity.notFound().build(); } String contentType = Files.probeContentType(file.toPath()); if (contentType == null) contentType = "image/jpeg"; return ResponseEntity.ok() - .contentType(MediaType.parseMediaType(contentType)) - .body(new FileSystemResource(file)); + .contentType(MediaType.parseMediaType(contentType)) + .body(new FileSystemResource(file)); } // 兼容旧路径:/2026/04/01/xxx.jpg @GetMapping("/legacy/**") public ResponseEntity getLegacyImage(HttpServletRequest request) throws IOException { String path = request.getRequestURI().replace("/api/upload/legacy", ""); - File file = new File(UPLOAD_BASE + path); + String basePath = uploadPath.endsWith("/") ? uploadPath : uploadPath + "/"; + File file = new File(basePath + path); if (!file.exists()) { return ResponseEntity.notFound().build(); } String contentType = Files.probeContentType(file.toPath()); if (contentType == null) contentType = "image/jpeg"; return ResponseEntity.ok() - .contentType(MediaType.parseMediaType(contentType)) - .body(new FileSystemResource(file)); + .contentType(MediaType.parseMediaType(contentType)) + .body(new FileSystemResource(file)); } @PostMapping("/image") @@ -78,9 +78,9 @@ public class FileController { } try { - // 创建上传目录(使用绝对路径) + // 创建上传目录 String datePath = LocalDate.now().toString().replace("-", "/"); - String dirPath = UPLOAD_BASE + datePath; // /.../uploads/2026/04/01 + String dirPath = uploadPath.endsWith("/") ? uploadPath + datePath : uploadPath + "/" + datePath; File dir = new File(dirPath); if (!dir.exists()) dir.mkdirs(); diff --git a/src/main/java/com/petstore/controller/ReportController.java b/src/main/java/com/petstore/controller/ReportController.java index 192f2c9..a48e5be 100644 --- a/src/main/java/com/petstore/controller/ReportController.java +++ b/src/main/java/com/petstore/controller/ReportController.java @@ -6,6 +6,7 @@ import com.petstore.service.ReportService; import com.petstore.service.StoreService; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; +import org.springframework.beans.factory.annotation.Value; import java.util.HashMap; import java.util.List; @@ -20,12 +21,13 @@ public class ReportController { private final ReportService reportService; private final StoreService storeService; - private static final String BASE_URL = "http://localhost:8080"; + @Value("${app.base-url:http://localhost:8080}") + private String baseUrl; private String fullUrl(String path) { if (path == null || path.isEmpty()) return path; if (path.startsWith("http")) return path; - return BASE_URL + path; + return baseUrl + path; } @PostMapping("/create")