Explorar o código

场景下载速度优化

dengsixing %!s(int64=3) %!d(string=hai) anos
pai
achega
004280ebc7

+ 43 - 9
4dkankan-center-scene-download/src/main/java/com/fdkankan/download/service/impl/SceneDownloadHandlerServiceImpl.java

@@ -28,6 +28,7 @@ import com.fdkankan.download.bean.ImageTypeDetail;
 import com.google.common.collect.Lists;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.UnsupportedEncodingException;
 import java.math.BigDecimal;
 import java.net.URLEncoder;
 import java.util.ArrayList;
@@ -387,7 +388,7 @@ public class SceneDownloadHandlerServiceImpl {
                 }
             }
 
-            for (ImageTypeDetail item : items) {
+            items.parallelStream().forEach(item->{
                 String par = "?x-oss-process=image/resize,m_lfit,w_" + imageType.getSize() + "/crop,w_512,h_512,x_" + item.getX() + ",y_" + item.getY();
                 if(StorageType.AWS.code().equals(uploadType)){
                     par += "&imagesVersion="+ imagesVersion;
@@ -401,18 +402,51 @@ public class SceneDownloadHandlerServiceImpl {
                         url += par;
                         break;
                     case AWS:
-                        url += URLEncoder.encode(par.replace("/", "@"), "UTF-8");
+                        try {
+                            url += URLEncoder.encode(par.replace("/", "@"), "UTF-8");
+                        } catch (UnsupportedEncodingException e) {
+                            e.printStackTrace();
+                        }
                         break;
                 }
                 var fky = key.split("/" + resolution + "/")[0] + "/" + dir + "/" + imageType.getName() +  num + "_" + item.getI()  + "_" + item.getJ() + ext;
-                if(imgKeys.contains(fky)){
-                    continue;
+                if(!imgKeys.contains(fky)){
+                    imgKeys.add(fky);
+                    long start = Calendar.getInstance().getTimeInMillis();
+                    try {
+                        this.zipBytes(out, wwwroot + fky, FileUtils.getBytesFromUrl(url));
+                    } catch (Exception e) {
+                        e.printStackTrace();
+                    }
+                    log.info("切图耗时,url:{},time:{}", url, Calendar.getInstance().getTimeInMillis() - start);
                 }
-                imgKeys.add(fky);
-                long start = Calendar.getInstance().getTimeInMillis();
-                this.zipBytes(out, wwwroot + fky, FileUtils.getBytesFromUrl(url));
-                log.info("切图耗时,url:{},time:{}", url, Calendar.getInstance().getTimeInMillis() - start);
-            }
+            });
+//            for (ImageTypeDetail item : items) {
+//                String par = "?x-oss-process=image/resize,m_lfit,w_" + imageType.getSize() + "/crop,w_512,h_512,x_" + item.getX() + ",y_" + item.getY();
+//                if(StorageType.AWS.code().equals(uploadType)){
+//                    par += "&imagesVersion="+ imagesVersion;
+//                }
+//
+//                var url = this.
+//                    resourceUrl + key;
+//                StorageType storageType = StorageType.get(uploadType);
+//                switch (storageType){
+//                    case OSS:
+//                        url += par;
+//                        break;
+//                    case AWS:
+//                        url += URLEncoder.encode(par.replace("/", "@"), "UTF-8");
+//                        break;
+//                }
+//                var fky = key.split("/" + resolution + "/")[0] + "/" + dir + "/" + imageType.getName() +  num + "_" + item.getI()  + "_" + item.getJ() + ext;
+//                if(imgKeys.contains(fky)){
+//                    continue;
+//                }
+//                imgKeys.add(fky);
+//                long start = Calendar.getInstance().getTimeInMillis();
+//                this.zipBytes(out, wwwroot + fky, FileUtils.getBytesFromUrl(url));
+//                log.info("切图耗时,url:{},time:{}", url, Calendar.getInstance().getTimeInMillis() - start);
+//            }
 
         }