From 129a9ce996248849e5156f25918ad06f33904af0 Mon Sep 17 00:00:00 2001 From: zch Date: Tue, 19 Nov 2024 10:20:08 +0800 Subject: [PATCH] =?UTF-8?q?change(module):=20=E6=B7=BB=E5=8A=A0=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=B7=A1=E6=A3=80=E5=BD=95=E5=83=8F=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 RecordInspectionCabinetController 中添加 getVisibleVideo 方法 - 通过路径参数获取视频文件并返回 - 支持动态检测文件 MIME 类型 - 在 RuoYiConfig 中添加 myPath 配置项用于指定文件路径 --- .../com/ruoyi/common/config/RuoYiConfig.java | 3 + .../RecordInspectionCabinetController.java | 74 +++++++++---------- 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/config/RuoYiConfig.java b/ruoyi-common/src/main/java/com/ruoyi/common/config/RuoYiConfig.java index edcf1aa..de3e1a9 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/config/RuoYiConfig.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/config/RuoYiConfig.java @@ -30,6 +30,8 @@ public class RuoYiConfig /** 验证码类型 */ private static String captchaType; + + // 自定义路径,用于获取日志信息里的文件 private static String myPath; public static String getMyPath() @@ -43,6 +45,7 @@ public class RuoYiConfig } + public String getName() { return name; diff --git a/ruoyi-module/src/main/java/com/ruoyi/record/controller/RecordInspectionCabinetController.java b/ruoyi-module/src/main/java/com/ruoyi/record/controller/RecordInspectionCabinetController.java index 9cc1545..5e04899 100644 --- a/ruoyi-module/src/main/java/com/ruoyi/record/controller/RecordInspectionCabinetController.java +++ b/ruoyi-module/src/main/java/com/ruoyi/record/controller/RecordInspectionCabinetController.java @@ -164,46 +164,46 @@ public class RecordInspectionCabinetController extends BaseController } } -/** - * 处理GET请求,返回指定路径的视频文件。 - * @param filePath 视频文件的路径 - * @return ResponseEntity 包含视频文件资源的响应实体 - */ -@GetMapping("/getVisibleVideo/{filePath}") -public ResponseEntity getVisibleVideo(@PathVariable String filePath) { - // 获取配置文件中的路径application.yml的profile - String myPath = RuoYiConfig.getMyPath(); - // 构建完整的文件路径 - String fileUrl = myPath + "/日志信息/巡检录像/可见光/" + filePath; - // 创建文件对象 - File file = new File(fileUrl); - // 检查文件是否存在 - if (file.exists()) { - // 如果文件存在,创建文件系统资源对象 - FileSystemResource resource = new FileSystemResource(file); - // 设置HTTP头信息 - HttpHeaders headers = new HttpHeaders(); - // 设置文件长度 - headers.setContentLength(file.length()); - // 动态检测文件的MIME类型 - try { - String contentType = Files.probeContentType(file.toPath()); - if (contentType != null) { - headers.setContentType(MediaType.parseMediaType(contentType)); - } else { + /** + * 处理GET请求,返回指定路径的视频文件。 + * @param filePath 视频文件的路径 + * @return ResponseEntity 包含视频文件资源的响应实体 + */ + @GetMapping("/getVisibleVideo/{filePath}") + public ResponseEntity getVisibleVideo(@PathVariable String filePath) { + // 获取配置文件中的路径application.yml的profile + String myPath = RuoYiConfig.getMyPath(); + // 构建完整的文件路径 + String fileUrl = myPath + "/日志信息/巡检录像/可见光/" + filePath; + // 创建文件对象 + File file = new File(fileUrl); + // 检查文件是否存在 + if (file.exists()) { + // 如果文件存在,创建文件系统资源对象 + FileSystemResource resource = new FileSystemResource(file); + // 设置HTTP头信息 + HttpHeaders headers = new HttpHeaders(); + // 设置文件长度 + headers.setContentLength(file.length()); + // 动态检测文件的MIME类型 + try { + String contentType = Files.probeContentType(file.toPath()); + if (contentType != null) { + headers.setContentType(MediaType.parseMediaType(contentType)); + } else { + headers.setContentType(MediaType.parseMediaType("video/*")); + } + } catch (IOException e) { headers.setContentType(MediaType.parseMediaType("video/*")); } - } catch (IOException e) { - headers.setContentType(MediaType.parseMediaType("video/*")); + // 返回包含文件资源的响应实体 + return ResponseEntity.ok() + .headers(headers) + .body(resource); + } else { + // 如果文件不存在,返回404状态 + return ResponseEntity.notFound().build(); } - // 返回包含文件资源的响应实体 - return ResponseEntity.ok() - .headers(headers) - .body(resource); - } else { - // 如果文件不存在,返回404状态 - return ResponseEntity.notFound().build(); } -} }