Browse Source

将获取文件的接口做额外的处理2

Yangzw 1 month ago
parent
commit
1c07d3b222

+ 4 - 0
feifan-framework/feifan-common/src/main/java/cn/newfeifan/mall/framework/common/util/servlet/ServletUtils.java

@@ -4,6 +4,7 @@ import cn.hutool.core.io.IoUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.extra.servlet.ServletUtil;
 import cn.newfeifan.mall.framework.common.util.json.JsonUtils;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.http.MediaType;
 import org.springframework.web.context.request.RequestAttributes;
 import org.springframework.web.context.request.RequestContextHolder;
@@ -26,6 +27,7 @@ import java.util.Map;
  *
  * @author 非繁源码
  */
+@Slf4j
 public class ServletUtils {
 
     /**
@@ -73,6 +75,7 @@ public class ServletUtils {
     public static void transfer(byte[] content, String reqRange, HttpServletResponse response){
         try {
             File sf = fileTransition(content);
+            log.info("=====file文件:{}",sf);
             RspInfo ri= getPartFileRsp( sf, reqRange) ;
             response.setContentLength((int)ri.content_Length);
             response.setHeader("Content-Range", ri.content_Range);
@@ -154,6 +157,7 @@ public class ServletUtils {
     public static File fileTransition(byte[] content) throws IOException{
         File file = new File("data/file/videoTest.mp4");
         if(!file.exists()){
+            log.info("=====创建文件");
             file.mkdir();
         }
         Files.write(Paths.get("data/file/videoTest.mp4"), content);

+ 7 - 7
feifan-module-infra/feifan-module-infra-biz/src/main/java/cn/newfeifan/mall/module/infra/controller/admin/file/FileController.java

@@ -81,14 +81,14 @@ public class FileController {
             response.setStatus(HttpStatus.NOT_FOUND.value());
             return;
         }
-//        if(path.contains("mp4")){
-//            String range = request.getHeader("Range");
-//            System.out.println("========Range:" + range);
-//            ServletUtils.transfer(content, range, response);
-//        } else {
-//        }
-        log.info("===============开始获取文件");
+        if (path.contains("mp4")) {
+            String range = request.getHeader("Range");
+            log.info("========Range:{}", range);
+            ServletUtils.transfer(content, range, response);
+        } else {
+            log.info("===============开始获取文件");
             ServletUtils.writeAttachment(response, path, content, request);
+        }
     }
 
     @GetMapping("/page")