Browse Source

下载优化

dengsixing 3 years ago
parent
commit
ed1438f88a

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

@@ -3,6 +3,7 @@ package com.fdkankan.download.service.impl;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.collection.ConcurrentHashSet;
 import cn.hutool.core.exceptions.ExceptionUtil;
+import cn.hutool.core.io.IoUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
@@ -29,7 +30,12 @@ import com.fdkankan.scene.api.feign.SceneUserSceneClient;
 import com.google.common.collect.Lists;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
 import java.math.BigDecimal;
+import java.net.HttpURLConnection;
+import java.net.URL;
 import java.net.URLEncoder;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
@@ -89,6 +95,9 @@ public class SceneDownloadHandlerServiceImpl {
     @Value("${path.v4school}")
     private String v4localPath;
 
+    @Value("${path.oss-local}")
+    private String ossLocalFormat;
+
     @Value("${path.zip-local}")
     private String zipLocalFormat;
 
@@ -376,29 +385,6 @@ public class SceneDownloadHandlerServiceImpl {
         return map;
     }
 
-    private JSONObject zipGetInfoJson(ZipOutputStream out, String root, String num) throws Exception{
-
-        ResultData<SceneInfoDTO> sceneViewInfo = sceneUserSceneClient.getSceneViewInfo(num);
-        if(!sceneViewInfo.getSuccess()){
-            throw new Exception(ServerCode.FEIGN_REQUEST_FAILD.message());
-        }
-        SceneInfoDTO data = sceneViewInfo.getData();
-        JSONObject getInfoJson = null;
-        if(Objects.isNull(data)){
-            getInfoJson = new JSONObject();
-        }else {
-            getInfoJson = JSONUtil.parseObj(data);
-        }
-
-        getInfoJson.set("sceneScheme", 3);
-        getInfoJson.set("needKey", 0);
-        getInfoJson.set("sceneKey","");
-        //写入getInfo.json
-        String getInfoJsonPath = root + String.format(UploadFilePath.DATA_VIEW_PATH, num) + "getInfo.json";
-        this.zipBytes(out, getInfoJsonPath, getInfoJson.toString().getBytes());
-        return getInfoJson;
-    }
-
     private void zipSceneJson(ZipOutputStream out, String root, String num, JSONObject sceneJson) throws Exception{
 
         //访问密码置0
@@ -494,7 +480,7 @@ public class SceneDownloadHandlerServiceImpl {
 
             zipBytes(out, prefix + key, content.getBytes());
         }else{
-            zipBytes(out, prefix + key, FileUtils.getBytesFromUrl(url));
+            zipFromUrl(out, prefix + key, url);
         }
     }
 
@@ -535,7 +521,7 @@ public class SceneDownloadHandlerServiceImpl {
 
     }
 
-    public void zipInputStream(ZipOutputStream out, String key, FileInputStream in) throws Exception {
+    public synchronized void zipInputStream(ZipOutputStream out, String key, InputStream in) throws Exception {
         out.putNextEntry(new org.apache.tools.zip.ZipEntry(key));
         byte[] bytes = new byte[1024];
         int b = 0;
@@ -550,4 +536,30 @@ public class SceneDownloadHandlerServiceImpl {
     }
 
 
+
+    public void zipFromUrl(ZipOutputStream out, String key, String url) throws Exception {
+        try (InputStream is = this.getInputStreamFromUrl(url)){
+            this.zipInputStream(out, key, is);
+        }catch (Exception e){
+            log.error("追加压缩包失败,url="+url, e);
+            throw e;
+        }
+    }
+
+    public InputStream getInputStreamFromUrl(String urlStr){
+        InputStream inputStream = null;
+        try {
+            URL url = new URL(urlStr);
+            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
+            conn.setConnectTimeout(3000);
+            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
+            inputStream = conn.getInputStream();
+            return inputStream;
+        } catch (FileNotFoundException e) {
+            log.error("获取流失败,url = " + urlStr, e);
+        } catch (IOException e) {
+            log.error("获取流失败,url = " + urlStr, e);
+        }
+        return null;
+    }
 }