Browse Source

下载中文名文件,解决war包失败。

xuxinyi 1 year ago
parent
commit
ae87dbadc9

+ 8 - 5
pom.xml

@@ -7,7 +7,7 @@
     <version>0.0.1-SNAPSHOT</version>
     <name>demo</name>
     <description>demo</description>
-    <packaging>war</packaging> <!-- 添加打包类型为 WAR -->
+    <packaging>war</packaging>
 
     <properties>
         <java.version>1.8</java.version>
@@ -29,20 +29,19 @@
         <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
-            <version>1.18.26</version> <!-- 确保使用可用版本 -->
+            <version>1.18.26</version>
             <scope>provided</scope>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-tomcat</artifactId>
-            <scope>provided</scope> <!-- 添加 Tomcat 依赖 -->
+            <scope>provided</scope>
         </dependency>
         <dependency>
             <groupId>com.baomidou</groupId>
             <artifactId>mybatis-plus-boot-starter</artifactId>
             <version>3.5.7</version>
         </dependency>
-
         <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
@@ -74,6 +73,11 @@
                     <encoding>UTF-8</encoding>
                 </configuration>
             </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-war-plugin</artifactId>
+                <version>3.3.1</version> <!-- 使用最新版本 -->
+            </plugin>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
@@ -92,5 +96,4 @@
             </plugin>
         </plugins>
     </build>
-
 </project>

+ 8 - 3
src/main/java/com/example/demo/controllers/FileController.java

@@ -7,6 +7,8 @@ import org.springframework.web.multipart.MultipartFile;
 
 import java.io.File;
 import java.io.IOException;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -31,7 +33,6 @@ public class FileController {
             return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件删除失败");
         }
     }
-
     // 获取文件列表,支持分页和搜索
     @GetMapping
     public ResponseEntity<List<String>> listFiles(
@@ -86,17 +87,21 @@ public class FileController {
         }
     }
 
-    // 文件下载
     @GetMapping("/download/{filename}")
     public ResponseEntity<byte[]> downloadFile(@PathVariable String filename) {
         try {
             Path path = Paths.get(uploadDir + filename);
             byte[] data = Files.readAllBytes(path);
+
+            // 对文件名进行编码
+            String encodedFilename = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
+
             return ResponseEntity.ok()
-                    .header("Content-Disposition", "attachment; filename=\"" + filename + "\"")
+                    .header("Content-Disposition", "attachment; filename=\"" + encodedFilename + "\"; filename*=UTF-8''" + encodedFilename)
                     .body(data);
         } catch (IOException e) {
             return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
         }
     }
+
 }

+ 5 - 5
src/main/resources/static/index.html

@@ -101,7 +101,7 @@
     const pageSize = 6;
 
     function loadFileList() {
-        fetch(`/api/files?page=${currentPage}&size=${pageSize}`)
+        fetch(`/demo/api/files?page=${currentPage}&size=${pageSize}`)
             .then(response => response.json())
             .then(files => {
                 const fileList = document.getElementById('fileList');
@@ -126,7 +126,7 @@
         const formData = new FormData();
         formData.append('file', file);
 
-        fetch('/api/files/upload', {
+        fetch('/demo/api/files/upload', {
             method: 'POST',
             body: formData
         })
@@ -140,12 +140,12 @@
     }
 
     function downloadFile(filename) {
-        window.location.href = '/api/files/download/' + filename;
+        window.location.href = '/demo/api/files/download/' + filename;
     }
 
     function deleteFile(filename) {
         if (confirm(`确定要删除文件 ${filename} 吗?`)) {
-            fetch(`/api/files/delete/${filename}`, {
+            fetch(`/demo/api/files/delete/${filename}`, {
                 method: 'DELETE'
             })
                 .then(response => response.text())
@@ -159,7 +159,7 @@
 
     function searchFiles() {
         const searchQuery = document.getElementById('searchInput').value;
-        fetch(`/api/files?search=${searchQuery}&page=0&size=${pageSize}`)
+        fetch(`/demo/api/files?search=${searchQuery}&page=0&size=${pageSize}`)
             .then(response => response.json())
             .then(files => {
                 const fileList = document.getElementById('fileList');