Kaynağa Gözat

添加下载文件流接口

gaohp 1 yıl önce
ebeveyn
işleme
f73a235c57

+ 9 - 0
feifan-framework/feifan-spring-boot-starter-web/src/main/java/cn/newfeifan/mall/framework/xss/config/FeifanXssAutoConfiguration.java

@@ -14,9 +14,13 @@ import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilde
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.boot.web.servlet.FilterRegistrationBean;
 import org.springframework.context.annotation.Bean;
+import org.springframework.http.converter.HttpMessageConverter;
+import org.springframework.http.converter.ResourceHttpMessageConverter;
 import org.springframework.util.PathMatcher;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
+import java.util.List;
+
 import static cn.newfeifan.mall.framework.web.config.FeifanWebAutoConfiguration.createFilterBean;
 
 @AutoConfiguration
@@ -58,4 +62,9 @@ public class FeifanXssAutoConfiguration implements WebMvcConfigurer {
         return createFilterBean(new XssFilter(properties, pathMatcher, xssCleaner), WebFilterOrderEnum.XSS_FILTER);
     }
 
+    @Override
+    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
+        converters.add(new ResourceHttpMessageConverter());
+    }
+
 }

+ 1 - 0
feifan-module-distri/feifan-module-distri-biz/src/main/java/cn/newfeifan/mall/module/distri/controller/admin/duser/vo/DuserInfoVO.java

@@ -23,4 +23,5 @@ public class DuserInfoVO {
 
     @Schema(description = "团队历史总贡献值=直推奖+合赢奖", example = "88")
     private Integer descTotalPrice;
+
 }

+ 56 - 3
feifan-module-infra/feifan-module-infra-biz/src/main/java/cn/newfeifan/mall/module/infra/controller/app/file/AppFileController.java

@@ -1,22 +1,34 @@
 package cn.newfeifan.mall.module.infra.controller.app.file;
 
 import cn.hutool.core.io.IoUtil;
+import cn.hutool.core.io.resource.InputStreamResource;
 import cn.newfeifan.mall.framework.common.pojo.CommonResult;
 import cn.newfeifan.mall.module.infra.controller.app.file.vo.AppFileUploadReqVO;
 import cn.newfeifan.mall.module.infra.service.file.FileService;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.core.io.ByteArrayResource;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.util.StreamUtils;
 import org.springframework.validation.annotation.Validated;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.client.RestTemplate;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
 
 import static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;
 
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
+
 @Tag(name = "用户 App - 文件存储")
 @RestController
 @RequestMapping("/infra/file")
@@ -27,6 +39,10 @@ public class AppFileController {
     @Resource
     private FileService fileService;
 
+
+    @Resource
+    private RestTemplate restTemplate;
+
     @PostMapping("/upload")
     @Operation(summary = "上传文件")
     public CommonResult<String> uploadFile(AppFileUploadReqVO uploadReqVO) throws Exception {
@@ -35,4 +51,41 @@ public class AppFileController {
         return success(fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())));
     }
 
+    @GetMapping("/downloadByUrl")
+    @Operation(summary = "通过链接下载文件流")
+    public ResponseEntity<ByteArrayResource> downloadByUrl(@RequestParam("fileUrl") String fileUrl) throws Exception {
+        try {
+            URL url = new URL(fileUrl);
+            URLConnection connection = url.openConnection();
+            byte[] data = StreamUtils.copyToByteArray(connection.getInputStream());
+            ByteArrayResource resource = new ByteArrayResource(data);
+
+            String[] path = fileUrl.split("/?\\?", 2)[0].split("/");  // Ignore query parameters
+            String filename = path[path.length - 1];
+
+            HttpHeaders headers = new HttpHeaders();
+            headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename);
+            headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
+            headers.add("Pragma", "no-cache");
+            headers.add("Expires", "0");
+
+            MediaType contentType = MediaType.APPLICATION_OCTET_STREAM; // Default content type
+            try {
+                contentType = MediaType.parseMediaType(connection.getContentType());
+            } catch (IllegalArgumentException ex) {
+                // Log warning and fall back to default content type
+            }
+
+            return ResponseEntity.ok()
+                    .headers(headers)
+                    .contentLength(connection.getContentLengthLong())
+                    .contentType(contentType)
+                    .body(resource);
+        } catch (IOException e) {
+            return null;
+        }
+    }
+
+
+
 }

+ 4 - 0
feifan-server/src/main/java/cn/newfeifan/mall/server/controller/DefaultController.java

@@ -47,4 +47,8 @@ public class DefaultController {
                 "[支付模块 feifan-module-pay - 已禁用][参考 https://doc.iocoder.cn/pay/build/ 开启]");
     }
 
+
+
+
+
 }