PicDownloadFaced.java 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package com.shanghaichengdi.downloadcenterserver.faced;
  2. import com.shanghaichengdi.downloadcentercommon.util.ImageDownload;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.net.URL;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9. import java.util.List;
  10. import lombok.extern.slf4j.Slf4j;
  11. /**
  12. * @author gengaoliu
  13. */
  14. @Slf4j
  15. public class PicDownloadFaced extends Thread {
  16. private List<String> imageUrls;
  17. private String passTime;
  18. public PicDownloadFaced(List<String> imageUrls, String passTime) {
  19. this.imageUrls = imageUrls;
  20. this.passTime = passTime;
  21. }
  22. @Override
  23. public void run() {
  24. String time = passTime;
  25. Date times = null;
  26. try {
  27. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  28. times =
  29. formatter.parse(time);
  30. } catch (ParseException e) {
  31. e.printStackTrace();
  32. }
  33. SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
  34. SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMdd_HHmmss");
  35. String dir = formatter.format(times);
  36. String newTime = formatter1.format(times);
  37. String year = dir.split("\\_")[0];
  38. String month = dir.split("\\_")[1];
  39. String day = dir.split("\\_")[2];
  40. String hour = dir.split("\\_")[3];
  41. String dirs = year + "/" + year + month + day + "/" + hour;
  42. int i = 0;
  43. File photoDir = new File("/shipAnalysis/HDImages" + dirs);
  44. photoDir.mkdirs();
  45. for (String imageUrl : imageUrls) {
  46. boolean isSuccess = false;
  47. URL url = null;
  48. try {
  49. String photoFile = "/Users/gengaoliu/" + dirs + "/" + "P" + i + "_" + newTime + ".jpg";
  50. while (!isSuccess) {
  51. try {
  52. isSuccess = ImageDownload.download(imageUrl, photoFile);
  53. } catch (Exception e) {
  54. log.error(e.getMessage());
  55. e.printStackTrace();
  56. }
  57. }
  58. i++;
  59. } catch (Exception e) {
  60. log.error(e.getMessage());
  61. e.printStackTrace();
  62. }
  63. }
  64. }
  65. }