Quellcode durchsuchen

针对下载文件名包含中文进行编码。

tomatozq vor 5 Jahren
Ursprung
Commit
b2609e754e

+ 22 - 2
picc-common/src/main/java/com/jpsoft/picc/modules/common/utils/OSSUtil.java

@@ -16,7 +16,9 @@ import org.apache.commons.lang3.StringUtils;
 import java.io.*;
 import java.net.URL;
 import java.net.URLConnection;
+import java.net.URLEncoder;
 import java.util.*;
+import java.util.stream.Collectors;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
 
@@ -67,7 +69,7 @@ public class OSSUtil {
         try {
             FileOutputStream output = new FileOutputStream(filePath);
 
-            URL url = new URL(fileUrl);
+            URL url = new URL(encodeFileName(fileUrl));
             URLConnection conn = url.openConnection();
             InputStream input = conn.getInputStream();
 
@@ -121,7 +123,8 @@ public class OSSUtil {
                     ZipEntry zipEntry = new ZipEntry(zipFile);
                     zos.putNextEntry(zipEntry);
 
-                    URL url = new URL(fileUrl);
+                    URL url = new URL(encodeFileName(fileUrl));
+
                     URLConnection conn = url.openConnection();
                     InputStream inputStream = conn.getInputStream();
 
@@ -148,6 +151,23 @@ public class OSSUtil {
         }
     }
 
+    private static String encodeFileName(String fileUrl) throws Exception{
+        String[] segements = fileUrl.split("\\?");
+
+        String[] arr = segements[0].split("/");
+
+        //文件名可能是中文,用utf-8编码
+        arr[arr.length - 1] = URLEncoder.encode(arr[arr.length - 1],"UTF-8");
+
+        String encFileUrl = Arrays.stream(arr).collect(Collectors.joining("/"));
+
+        if (segements.length>1){
+            encFileUrl += "?" + segements[1];
+        }
+
+        return encFileUrl;
+    }
+
     public static void presignedDownload(OSSConfig ossConfig, List<Map> fileList, OutputStream output){
         BufferedInputStream bis = null;