DownloadFileJob.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package com.jiayue.client.job;
  2. import cn.hutool.core.convert.Convert;
  3. import cn.hutool.http.HttpRequest;
  4. import cn.hutool.http.HttpUtil;
  5. import cn.hutool.json.JSONArray;
  6. import cn.hutool.json.JSONObject;
  7. import cn.hutool.json.JSONUtil;
  8. import com.jiayue.client.entity.FileCreateLog;
  9. import com.jiayue.client.service.QueryDataService;
  10. import com.jiayue.client.util.FileUtil;
  11. import com.jiayue.client.util.Scpclient;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
  14. import org.springframework.core.io.PathResource;
  15. import org.springframework.scheduling.annotation.Scheduled;
  16. import org.springframework.stereotype.Service;
  17. import java.io.File;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.Properties;
  22. /**
  23. * 青海集中功率预测从云平台下载气象文件数据:
  24. * 1.每天凌晨5点执行,下载当天气象文件传给功率预测服务器
  25. */
  26. @Service
  27. @Slf4j
  28. public class DownloadFileJob {
  29. public final QueryDataService queryDataService;
  30. //获取下载到文件,从临时目录移动到目标目录
  31. public static List<String> filedownLoadList = new ArrayList<>();
  32. public static List<String> isDownFileId = new ArrayList<>();
  33. public static Properties properties;
  34. static {
  35. YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
  36. yaml.setResources(new PathResource("./focus-client.yml"));
  37. properties = yaml.getObject();
  38. }
  39. public DownloadFileJob(QueryDataService queryDataService) {
  40. this.queryDataService = queryDataService;
  41. }
  42. /**
  43. * 每天凌晨2点执行
  44. */
  45. @Scheduled(cron = "0 0/5 * * * ?")
  46. public void download() {
  47. log.info("上午文件下载定时任务开始");
  48. List<Map<String, Object>> stations = queryDataService.getStations();
  49. for (Map<String, Object> station : stations) {
  50. String stationCode = Convert.toStr(station.get("C_STATION_CODE"));
  51. log.info("开始执行定时任务:http下载场站:{} 的文件。", stationCode);
  52. String body = HttpRequest.get(properties.getProperty("v3cloud.filelogUrl") + stationCode).execute().body();
  53. JSONObject json = JSONUtil.parseObj(body);
  54. String code = json.get("code").toString();
  55. String data = json.get("data").toString();
  56. if (code.equals("0") && data.length() > 0) {
  57. JSONArray array = JSONUtil.parseArray(data);
  58. List<FileCreateLog> list = array.toList(FileCreateLog.class);
  59. String path = FileUtil.judeDirExists(properties.getProperty("local.downFilePath"));
  60. try {
  61. for (FileCreateLog fileCreateLog : list) {
  62. if (isDownFileId.contains(fileCreateLog.getId())) {
  63. log.info("文件:{}已下载。", fileCreateLog.getFileName());
  64. continue;
  65. }
  66. log.info("开始下载文件:{}。", fileCreateLog.getFileName());
  67. String url = properties.getProperty("v3cloud.fileDownUrl") + fileCreateLog.getId();// "https://117.78.19.70:9010/client/getFileById?id=" + fileCreateLog.getId();
  68. String targetPath = path + File.separatorChar +stationCode+File.separatorChar +"new"+File.separatorChar+ fileCreateLog.getFileName();
  69. long size = HttpUtil.downloadFile(url,targetPath);
  70. if (size > 0) {
  71. isDownFileId.add(fileCreateLog.getId());
  72. log.info("成功下载文件:{}", targetPath);
  73. filedownLoadList.add(targetPath);
  74. }
  75. }
  76. } catch (Exception e) {
  77. log.error("文件下载失败:{}", e);
  78. } finally {
  79. //把下载的文件挪到功率预测服务器文件夹
  80. if (filedownLoadList.size() > 0) {
  81. log.info("开始移动下载文件到功率预测,共:{} 个文件。", filedownLoadList.size());
  82. for (String fpath : filedownLoadList) {
  83. try {
  84. Scpclient scpclient = Scpclient.getInstance(properties.getProperty("remote.ip"),
  85. Convert.toInt(properties.getProperty("remote.port")), properties.getProperty("remote.user"),
  86. properties.getProperty("remote.pwd"));
  87. scpclient.putFile(fpath, properties.getProperty("remote.path") + stationCode + "/new/");
  88. log.info("移动文件:{} 到远程服务器 {} 成功", fpath, properties.getProperty("remote.path") + stationCode + "/new/");
  89. } catch (Exception e) {
  90. log.error("移动文件到功率预测服务器失败:{}", e);
  91. }
  92. }
  93. log.info("所有文件移动完成,清空文件临时filedownLoadList");
  94. filedownLoadList.clear();
  95. } else {
  96. log.info("没有新文件下载,不需要移动到远程功率预测服务器。");
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }