PicDownloadFaced.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package com.shanghaichengdi.downloadcenterserver.faced;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.shanghaichengdi.downloadcentercommon.util.ImageDownload;
  4. import com.shanghaichengdi.downloadcenterserver.cache.UrlData;
  5. import java.io.File;
  6. import java.net.URL;
  7. import java.text.ParseException;
  8. import java.text.SimpleDateFormat;
  9. import java.util.ArrayList;
  10. import java.util.Date;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.stream.Collectors;
  15. import lombok.Getter;
  16. import lombok.extern.slf4j.Slf4j;
  17. /**
  18. * @author gengaoliu
  19. */
  20. @Slf4j
  21. public class PicDownloadFaced extends Thread {
  22. private List<String> imageUrls;
  23. private String passTime;
  24. private String id;
  25. @Getter
  26. private String bayonetId;
  27. public PicDownloadFaced(List<String> imageUrls, String passTime, String id, String bayonetId) {
  28. this.imageUrls = imageUrls;
  29. this.passTime = passTime;
  30. this.id = id;
  31. this.bayonetId = bayonetId;
  32. }
  33. @Override
  34. public void run() {
  35. List<String> imgUrlToSave = new ArrayList<>(this.imageUrls.size());
  36. String time = passTime;
  37. Date times = null;
  38. try {
  39. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  40. times =
  41. formatter.parse(time);
  42. } catch (ParseException e) {
  43. e.printStackTrace();
  44. }
  45. SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
  46. SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMdd_HHmmss");
  47. String dir = formatter.format(times);
  48. String newTime = formatter1.format(times);
  49. String year = dir.split("\\_")[0];
  50. String month = dir.split("\\_")[1];
  51. String day = dir.split("\\_")[2];
  52. String hour = dir.split("\\_")[3];
  53. String dirs = year + "/" + year + month + day + "/" + hour;
  54. int i = 0;
  55. File photoDir = new File("/shipAnalysis/HDImages/" + dirs);
  56. photoDir.mkdirs();
  57. for (String imageUrl : imageUrls) {
  58. boolean isSuccess = false;
  59. URL url = null;
  60. try {
  61. String urlToSave = dirs + "/" + "P" + i + "_" + newTime + ".jpg";
  62. String photoFile = "/shipAnalysis/HDImages/" + urlToSave;
  63. while (!isSuccess) {
  64. try {
  65. isSuccess = ImageDownload.download(imageUrl, photoFile);
  66. } catch (Exception e) {
  67. log.error(e.getMessage());
  68. e.printStackTrace();
  69. }
  70. }
  71. imgUrlToSave.add(urlToSave);
  72. i++;
  73. } catch (Exception e) {
  74. log.error(e.getMessage());
  75. e.printStackTrace();
  76. }
  77. }
  78. // log.warn("====> 当前队列值:{}", UrlData.URL_IMG_TO_SAVE);
  79. addQueue(id, imgUrlToSave);
  80. // log.warn("======> 取出并移除首位元素:{}", JSONObject.toJSONString(UrlData.URL_IMG_TO_SAVE.poll()));
  81. Map<String, List<String>> polled = UrlData.URL_IMG_TO_SAVE.poll();
  82. if (!"8".equals(bayonetId)) {
  83. String id = new ArrayList<>(polled.keySet()).get(0);
  84. AnalyseFaced.shipCoverAnalyse(id, polled.get(id).get(0), bayonetId);
  85. AnalyseFaced.lifeJacketAnalyse(id, polled.get(id), bayonetId);
  86. if (!"14".equals(bayonetId)) {
  87. AnalyseFaced.boatFlagAnalyse(id, polled.get(id), bayonetId);
  88. }
  89. }
  90. }
  91. private synchronized static void addQueue(String id, List<String> imgUrlToSave) {
  92. Map<String, List<String>> map = new HashMap<>();
  93. map.put(id, imgUrlToSave);
  94. UrlData.URL_IMG_TO_SAVE.add(map);
  95. }
  96. }