Procházet zdrojové kódy

视频文件相关修改

chenjun před 11 měsíci
rodič
revize
4d8eeffa51

+ 45 - 0
zjugis-module-infra/zjugis-module-infra-biz/src/main/java/com/zjugis/module/infra/controller/admin/file/FileController.java

@@ -27,6 +27,7 @@ import javax.annotation.security.PermitAll;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.validation.Valid;
+import java.net.URLEncoder;
 import java.util.List;
 
 import static com.zjugis.framework.common.pojo.CommonResult.success;
@@ -98,6 +99,50 @@ public class FileController {
         ServletUtils.writeAttachment(response, path, content);
     }
 
+    @GetMapping("/{configId}/get/video/**")
+    @PermitAll
+    @Operation(summary = "下载文件")
+    @Parameter(name = "configId", description = "配置编号",  required = true)
+    public void getVideoFileContent(HttpServletRequest request,
+                               HttpServletResponse response,
+                               @PathVariable("configId") Long configId) throws Exception {
+        // 获取请求的路径
+        String path = StrUtil.subAfter(request.getRequestURI(), "/get/video/", false);
+        if (StrUtil.isEmpty(path)) {
+            throw new IllegalArgumentException("结尾的 path 路径必须传递");
+        }
+
+        // 读取内容
+        byte[] content = fileService.getFileContent(configId, path);
+        if (content == null) {
+            log.warn("[getFileContent][configId({}) path({}) 文件不存在]", configId, path);
+            response.setStatus(HttpStatus.NOT_FOUND.value());
+            return;
+        }
+        String rangeString = request.getHeader("Range");
+        if (rangeString != null && rangeString.trim().length() > 0 && !"null".equals(rangeString)) {
+            long range = Long.valueOf(rangeString.substring(rangeString.indexOf("=") + 1, rangeString.indexOf("-")));
+
+            response.setHeader("Content-Type", "video/mp4");
+
+            //10000是视频文件的大小,上传文件时都会有这些参数的
+            response.setContentLength(content.length);
+
+            //拖动进度条时的断点,其中10000是上面的视频文件大小,改成你的就好
+            response.setHeader("Content-Range", String.valueOf(range + (content.length - 1)));
+
+            response.setHeader("Accept-Ranges", "bytes");
+
+        }
+        //告诉浏览器输出内容为流
+        response.setContentType("application/octet-stream");
+        // 设置文件名
+        response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(path, "UTF-8"));
+        response.addHeader("Content-Length", "" + content.length);
+        // 输出附件
+        IoUtil.write(response.getOutputStream(), false, content);
+    }
+
     @GetMapping("/page")
     @Operation(summary = "获得文件分页")
     @PreAuthorize("@ss.hasPermission('infra:file:query')")

+ 3 - 0
zjugis-module-infra/zjugis-module-infra-biz/src/main/java/com/zjugis/module/infra/service/file/FileServiceImpl.java

@@ -92,6 +92,9 @@ public class FileServiceImpl implements FileService {
         file.setName(name);
         file.setPath(path);
         file.setUrl(url);
+        if ("video/mp4".equals(type)){
+            file.setUrl(url.replace("/get/","/get/video/"));
+        }
         file.setType(type);
         file.setSize(content.length);
         fileMapper.insert(file);