Browse Source

:sparkles: 接口返回存储数据库路径

lag 1 year ago
parent
commit
998393f23f

+ 11 - 0
download-center-server/src/main/java/com/shanghaichengdi/downloadcenterserver/cache/UrlData.java

@@ -0,0 +1,11 @@
+package com.shanghaichengdi.downloadcenterserver.cache;
+
+import java.util.List;
+import java.util.concurrent.ConcurrentLinkedQueue;
+
+/**
+ * @author gengaoliu
+ */
+public class UrlData {
+  public static ConcurrentLinkedQueue<List<String>> URL_IMG_TO_SAVE = new ConcurrentLinkedQueue<>();
+}

+ 5 - 2
download-center-server/src/main/java/com/shanghaichengdi/downloadcenterserver/controller/PicDownloadController.java

@@ -3,6 +3,7 @@ package com.shanghaichengdi.downloadcenterserver.controller;
 import com.alibaba.fastjson.JSONObject;
 import com.shanghaichengdi.downloadcentercommon.util.ImageDownload;
 import com.shanghaichengdi.downloadcentercommon.util.SSLUtil;
+import com.shanghaichengdi.downloadcenterserver.cache.UrlData;
 import com.shanghaichengdi.downloadcenterserver.domain.PicDownload;
 import com.shanghaichengdi.downloadcenterserver.faced.PicDownloadFaced;
 import com.shanghaigeography.entity.ResultMsg;
@@ -39,10 +40,12 @@ public class PicDownloadController {
       faced.start();
     } catch (Exception e) {
       log.info("下载失败,原因:{}",e.getMessage());
-      return new ResultMsg<String>().setMsg("下载失败").setCode(ResultState.ERROR).toJSON();
+      return new ResultMsg<String>().setMsg("下载失败").setCode(ResultState.ERROR).setData(JSONObject.toJSONString(
+          UrlData.URL_IMG_TO_SAVE.poll())).toJSON();
     }
     log.info("下载成功");
-    return new ResultMsg<String>().setMsg("下载成功").setCode(ResultState.SUCCESS).toJSON();
+    return new ResultMsg<String>().setMsg("下载成功").setCode(ResultState.SUCCESS).setData(JSONObject.toJSONString(
+          UrlData.URL_IMG_TO_SAVE.poll())).toJSON();
   }
 
   @Deprecated

+ 11 - 1
download-center-server/src/main/java/com/shanghaichengdi/downloadcenterserver/faced/PicDownloadFaced.java

@@ -1,10 +1,12 @@
 package com.shanghaichengdi.downloadcenterserver.faced;
 
 import com.shanghaichengdi.downloadcentercommon.util.ImageDownload;
+import com.shanghaichengdi.downloadcenterserver.cache.UrlData;
 import java.io.File;
 import java.net.URL;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 import lombok.extern.slf4j.Slf4j;
@@ -24,6 +26,7 @@ public class PicDownloadFaced extends Thread {
 
   @Override
   public void run() {
+    List<String> imgUrlToSave = new ArrayList<>(this.imageUrls.size());
     String time = passTime;
     Date times = null;
     try {
@@ -49,7 +52,8 @@ public class PicDownloadFaced extends Thread {
       boolean isSuccess = false;
       URL url = null;
       try {
-        String photoFile = "/shipAnalysis/HDImages/" + dirs + "/" + "P" + i + "_" + newTime + ".jpg";
+        String urlToSave = dirs + "/" + "P" + i + "_" + newTime + ".jpg";
+        String photoFile = "/shipAnalysis/HDImages/" + urlToSave;
         while (!isSuccess) {
           try {
             isSuccess = ImageDownload.download(imageUrl, photoFile);
@@ -58,11 +62,17 @@ public class PicDownloadFaced extends Thread {
             e.printStackTrace();
           }
         }
+        imgUrlToSave.add(urlToSave);
         i++;
       } catch (Exception e) {
         log.error(e.getMessage());
         e.printStackTrace();
       }
     }
+    addQueue(imgUrlToSave);
+  }
+
+  private synchronized static void addQueue(List<String> imgUrlToSave) {
+    UrlData.URL_IMG_TO_SAVE.add(imgUrlToSave);
   }
 }