package com.shanghaichengdi.downloadcenterserver.faced; import com.alibaba.fastjson.JSONObject; 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.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import lombok.Getter; import lombok.extern.slf4j.Slf4j; /** * @author gengaoliu */ @Slf4j public class PicDownloadFaced extends Thread { private List imageUrls; private String passTime; private String id; @Getter private String bayonetId; public PicDownloadFaced(List imageUrls, String passTime, String id, String bayonetId) { this.imageUrls = imageUrls; this.passTime = passTime; this.id = id; this.bayonetId = bayonetId; } @Override public void run() { List imgUrlToSave = new ArrayList<>(this.imageUrls.size()); String time = passTime; Date times = null; try { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); times = formatter.parse(time); } catch (ParseException e) { e.printStackTrace(); } SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMdd_HHmmss"); String dir = formatter.format(times); String newTime = formatter1.format(times); String year = dir.split("\\_")[0]; String month = dir.split("\\_")[1]; String day = dir.split("\\_")[2]; String hour = dir.split("\\_")[3]; String dirs = year + "/" + year + month + day + "/" + hour; int i = 0; File photoDir = new File("/shipAnalysis/HDImages/" + dirs); photoDir.mkdirs(); for (String imageUrl : imageUrls) { boolean isSuccess = false; URL url = null; try { String urlToSave = dirs + "/" + "P" + i + "_" + newTime + ".jpg"; String photoFile = "/shipAnalysis/HDImages/" + urlToSave; while (!isSuccess) { try { isSuccess = ImageDownload.download(imageUrl, photoFile); } catch (Exception e) { log.error(e.getMessage()); e.printStackTrace(); } } imgUrlToSave.add(urlToSave); i++; } catch (Exception e) { log.error(e.getMessage()); e.printStackTrace(); } } // log.warn("====> 当前队列值:{}", UrlData.URL_IMG_TO_SAVE); addQueue(id, imgUrlToSave); // log.warn("======> 取出并移除首位元素:{}", JSONObject.toJSONString(UrlData.URL_IMG_TO_SAVE.poll())); Map> polled = UrlData.URL_IMG_TO_SAVE.poll(); if (!"8".equals(bayonetId)) { String id = new ArrayList<>(polled.keySet()).get(0); AnalyseFaced.shipCoverAnalyse(id, polled.get(id).get(0), bayonetId); AnalyseFaced.lifeJacketAnalyse(id, polled.get(id), bayonetId); if (!"14".equals(bayonetId)) { AnalyseFaced.boatFlagAnalyse(id, polled.get(id), bayonetId); } } } private synchronized static void addQueue(String id, List imgUrlToSave) { Map> map = new HashMap<>(); map.put(id, imgUrlToSave); UrlData.URL_IMG_TO_SAVE.add(map); } }