Jelajahi Sumber

解决下载和读取接口有_的文档,导致下载类型出错

DESKTOP-2K9OVK9\siwei 4 bulan lalu
induk
melakukan
89f7c53926

+ 9 - 4
onemap-modules/onemap-file/src/main/java/com/onemap/file/controller/SysFileController.java

@@ -25,6 +25,7 @@ import org.springframework.web.servlet.HandlerMapping;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.io.File;
 import java.io.IOException;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
@@ -302,7 +303,9 @@ public class SysFileController {
             String uploadFile = spaceFileRecordService.uploadAbsolutePath(URLDecoder.decode(extractedPath, "UTF-8"));
             // 构建文件的完整路径
             Path filePath = Paths.get(uploadFile);
-            Resource resource = new FileSystemResource(filePath.toFile());
+            File file = filePath.toFile();
+            String d0_name = file.getName();
+            Resource resource = new FileSystemResource(file);
             // 如果资源不存在,返回404
             if (!resource.exists()) {
                 return ResponseEntity.notFound().build();
@@ -321,7 +324,7 @@ public class SysFileController {
 //                String headName = headNames.nextElement();
 //                headers.set(headName, request.getHeader(headName));
 //            }
-            headers.add(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + URLEncoder.encode(resource.getFilename(), "UTF-8") + "\"");
+            headers.add(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + URLEncoder.encode(d0_name, "UTF-8") + "\"");
             headers.setContentType(MediaType.parseMediaType(contentType));
 //            headers.setAccessControlAllowOrigin("*");
             // 返回文件资源
@@ -351,7 +354,9 @@ public class SysFileController {
             String uploadFile = spaceFileRecordService.uploadAbsolutePath(URLDecoder.decode(extractedPath, "UTF-8"));
             // 构建文件的完整路径
             Path filePath = Paths.get(uploadFile);
-            Resource resource = new FileSystemResource(filePath.toFile());
+            File file = filePath.toFile();
+            String d0_name = file.getName();
+            Resource resource = new FileSystemResource(file);
             // 如果资源不存在,返回404
             if (!resource.exists()) {
                 return ResponseEntity.notFound().build();
@@ -365,7 +370,7 @@ public class SysFileController {
             // 设置HTTP头部信息
             HttpHeaders headers = new HttpHeaders();
 //            headers.add(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + URLEncoder.encode(resource.getFilename(), "UTF-8") + "\"");
-            headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + URLEncoder.encode(resource.getFilename() + "\""));
+            headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + URLEncoder.encode(d0_name, "UTF-8") + "\"");
             headers.setContentType(MediaType.parseMediaType(contentType));
 //            headers.setAccessControlAllowOrigin("*");