From 44a5b01b928d4816d7bfafdbc8c50bae7eedaebb Mon Sep 17 00:00:00 2001
From: liujie <1793218484@qq.com>
Date: 星期二, 27 一月 2026 17:01:56 +0800
Subject: [PATCH] 大屏项目接口

---
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/ProjectController.java |   68 ++++++++++++++++++++++++++++++++++
 ruoyi-admin/src/main/resources/application-test.yml                           |    2 +
 ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java  |    1 
 3 files changed, 71 insertions(+), 0 deletions(-)

diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/ProjectController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/ProjectController.java
index c8ac7ec..9174fd2 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/ProjectController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/ProjectController.java
@@ -16,16 +16,28 @@
 import com.ruoyi.system.service.*;
 import com.ruoyi.system.vo.*;
 import io.swagger.annotations.*;
+import lombok.SneakyThrows;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.beans.factory.parsing.Problem;
+import org.springframework.core.io.InputStreamResource;
 import org.springframework.format.annotation.DateTimeFormat;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 
+import javax.annotation.PostConstruct;
+import javax.annotation.Resource;
 import javax.validation.Valid;
+import java.io.IOException;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
+import java.net.URLEncoder;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -440,4 +452,60 @@
         return R.ok(list);
 
     }
+
+
+    @Resource
+    UserFileService userFileService;
+
+    @SneakyThrows
+    @ApiOperation(value = "下载文件(名称)")
+    @GetMapping("/download/{name}")
+    public ResponseEntity<org.springframework.core.io.Resource> downloadFileByName(@PathVariable String name) {
+        var userFile = userFileService.getOne(new LambdaQueryWrapper<UserFile>().eq(UserFile::getName, name));
+        return downloadFile(userFile);
+    }
+
+    private ResponseEntity<org.springframework.core.io.Resource> downloadFile(UserFile userFile) throws IOException {
+        InputStreamResource resource =resolveResource(userFile);
+        var fileSize = userFile.getSize();
+//        if(resource == null) {
+//            resource = new InputStreamResource(getClass().getClassLoader().getResourceAsStream("images/wukong.png"));
+//            fileSize = 6215360L;// resource.contentLength();
+//        }
+
+        String encodedFileName = URLEncoder.encode(userFile.getOriginFileName(), "UTF-8");
+        String headerValue = String.format("attachment; filename=\"%s\"", encodedFileName);
+        return ResponseEntity.ok()
+                .contentType(MediaType.parseMediaType(userFile.getContentType()))
+                .contentLength(fileSize)
+                .header(HttpHeaders.CONTENT_DISPOSITION, headerValue)
+                .body(resource);
+    }
+
+    public InputStreamResource resolveResource(UserFile userFile) throws IOException {
+        var filePath = fileLocation.resolve(userFile.getName() + "." + userFile.getExtension()).normalize();
+        if(!Files.exists(filePath)){
+            return null;
+        }
+        return new InputStreamResource(Files.newInputStream(filePath));
+    }
+
+    private Path fileLocation;
+
+
+
+    @Value("${file.upload.dir}/files")
+    private String UPLOAD_DIR;
+
+
+    @PostConstruct
+    void init(){
+        fileLocation  = Path.of(UPLOAD_DIR).toAbsolutePath().normalize();
+        try {
+            Files.createDirectories(this.fileLocation);
+        } catch (Exception ex) {
+            throw new RuntimeException("Could not create the directory where the uploaded files will be stored.", ex);
+        }
+    }
+
 }
diff --git a/ruoyi-admin/src/main/resources/application-test.yml b/ruoyi-admin/src/main/resources/application-test.yml
index f6482a2..ffc467d 100644
--- a/ruoyi-admin/src/main/resources/application-test.yml
+++ b/ruoyi-admin/src/main/resources/application-test.yml
@@ -176,6 +176,7 @@
   # 请求前缀
   pathMapping: /
 
+
 # 防止XSS攻击
 xss:
   # 过滤开关
@@ -187,6 +188,7 @@
 # file upload
 file:
   upload:
+    dir: F:\file\estate
     location: /file/
     qrLocation: /file/qrCode/
     accessPath: /file/
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java
index 9dfb444..5472ed0 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java
@@ -119,6 +119,7 @@
                         "/index/**",
                         "/merchant/**",
                         "/openIdByJsCode","/wx/**","/html-config/*","/icbc-notify/**","/doc.html/**"
+                        ,"/project/download/**"
                 ).permitAll()
                 // 静态资源,可匿名访问
                 .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()

--
Gitblit v1.7.1