Browse Source

修改文件名转义

zhoupeng 1 year ago
parent
commit
5156de6349

+ 11 - 4
cqcy-ei/cqcy-ei-da/src/main/java/com/example/opc_da/controller/FileController.java

@@ -10,6 +10,7 @@ import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletResponse;
 import java.io.*;
+import java.net.URLEncoder;
 import java.util.Date;
 
 @RestController
@@ -37,6 +38,11 @@ public class FileController {
         String type = orgFilename.substring(orgFilename.lastIndexOf("."));
         String fileName = (new Date()).getTime() + ((int) (Math.random() * 90000) + 10000) + type;
         String newFilePath = filePath + fileName;
+        String fileSavePath = filePath;
+        File file1 = new File(fileSavePath);
+        if (!file1.exists() && !file1.isDirectory()) {
+            file1.mkdirs();
+        }
         File newFile = new File(newFilePath);
         try {
             file.transferTo(newFile);
@@ -47,16 +53,17 @@ public class FileController {
         XlsToPdfUtil.excelToPDF(newFilePath, outPath, new FileParam());
         newFile.delete();
 
-        String pdfFileName = orgFilename.substring(0, orgFilename.lastIndexOf("."))+ ".pdf";
-        JSONObject jsonObject=new JSONObject();
-        jsonObject.put("outPath",outPath);
-        jsonObject.put("pdfFileName",pdfFileName);
+        String pdfFileName = orgFilename.substring(0, orgFilename.lastIndexOf(".")) + ".pdf";
+        JSONObject jsonObject = new JSONObject();
+        jsonObject.put("outPath", outPath);
+        jsonObject.put("pdfFileName", pdfFileName);
         return Result.ok(jsonObject);
     }
 
     @RequestMapping(value = "/download", method = RequestMethod.GET)
     public void download(String outPath, String pdfFileName, HttpServletResponse response) {
         try {
+            pdfFileName = URLEncoder.encode(pdfFileName, "UTF-8");
             // path是指欲下载的文件的路径。
             File pdfFile = new File(outPath);
             // 以流的形式下载文件。