From fd770581a716d77471483a572b17a2a6b69966d9 Mon Sep 17 00:00:00 2001 From: NiuZiYuan Date: Sat, 17 May 2025 12:44:23 +0800 Subject: [PATCH] dev --- .../common/config/ControllerAspect.java | 4 ++- .../controller/common/FileController.java | 29 ++++++++++++------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/syjiaer/clinic/server/common/config/ControllerAspect.java b/src/main/java/com/syjiaer/clinic/server/common/config/ControllerAspect.java index 4fcb30b..25804d4 100644 --- a/src/main/java/com/syjiaer/clinic/server/common/config/ControllerAspect.java +++ b/src/main/java/com/syjiaer/clinic/server/common/config/ControllerAspect.java @@ -3,6 +3,7 @@ package com.syjiaer.clinic.server.common.config; import com.syjiaer.clinic.server.common.exception.MessageException; import com.syjiaer.clinic.server.common.exception.VerifyException; import com.syjiaer.clinic.server.common.vo.Result; +import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; @@ -11,6 +12,7 @@ import org.springframework.stereotype.Component; import java.net.http.HttpResponse; +@Slf4j @Component // 将当前类交给spring管理 @Aspect // 声明这是一个AOP类 public class ControllerAspect { @@ -40,7 +42,7 @@ public class ControllerAspect { result.setMessage("系统异常"); result.setData(null); result.setCode(101); - e.printStackTrace(); + log.error(e.getMessage()); return result; } } diff --git a/src/main/java/com/syjiaer/clinic/server/controller/common/FileController.java b/src/main/java/com/syjiaer/clinic/server/controller/common/FileController.java index beb947f..1b751eb 100644 --- a/src/main/java/com/syjiaer/clinic/server/controller/common/FileController.java +++ b/src/main/java/com/syjiaer/clinic/server/controller/common/FileController.java @@ -42,26 +42,33 @@ public class FileController extends BaseController { @RequestMapping("/getImage/{fileName}") @NoAuthCheck public ResponseEntity getImage(@PathVariable String fileName) { - return fileService.getImage(fileName); + try { + return fileService.getImage(fileName); + } catch (Exception e) { + return ResponseEntity.notFound().build(); + } + } @RequestMapping("/download/{token}") @NoAuthCheck public ResponseEntity download(@PathVariable String token){ - JSONObject jsonObject =cacheUtil.get(token); - String filename=jsonObject.getString("path"); - Path path = Paths.get(filename); - Resource resource= null; + try { + JSONObject jsonObject =cacheUtil.get(token); + String filename=jsonObject.getString("path"); + Path path = Paths.get(filename); + Resource resource= null; resource = new UrlResource(path.toUri()); - } catch (MalformedURLException e) { - throw new MessageException("创建下载失败"); + return ResponseEntity.ok() + .contentType(MediaType.parseMediaType("application/octet-stream")) + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"") + .body(resource); + } catch (Exception e) { + return ResponseEntity.notFound().build(); } - return ResponseEntity.ok() - .contentType(MediaType.parseMediaType("application/octet-stream")) - .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"") - .body(resource); + }