瀏覽代碼

2022年12月09日11:25:59

黄洋 2 年之前
父節點
當前提交
0b66e679b6

+ 8 - 10
tongfei_river_data_collection/src/main/java/com/ublinkage/datacollection/service/QxybDataCollectionTask.java

@@ -10,10 +10,8 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 
-import java.text.DateFormat;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
-import java.util.ArrayList;
 import java.util.Date;
 import java.util.LinkedList;
 import java.util.List;
@@ -59,14 +57,14 @@ public class QxybDataCollectionTask {
     public void getQxybData() {
         log.info("开始下载:{}到{}的预报数据.", this.getYbStartTime(), this.getYbEndTime());
         //下载分区雨量
-//        this.getForecast15Data();
-//
-//        this.getForecast29Data();
-//
-//        this.getDomainTempAndRain();
-//
-//        //获取网格数据,包含温度网格,降雨网格
-//        this.getGridData();
+        this.getForecast15Data();
+
+        this.getForecast29Data();
+
+        this.getDomainTempAndRain();
+
+        //获取网格数据,包含温度网格,降雨网格
+        this.getGridData();
 
         //下载温度站点
         this.getForeTempData();

+ 0 - 2
tongfei_river_data_collection/src/test/java/com/ublinkage/datacollection/MyTest.java

@@ -1,14 +1,12 @@
 package com.ublinkage.datacollection;
 
 import cn.hutool.core.io.FileUtil;
-import cn.hutool.core.util.ZipUtil;
 import cn.hutool.http.HttpUtil;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import org.junit.Test;
 
 import java.io.File;
-import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
 

+ 61 - 0
tongfei_river_data_transfer/src/main/java/com/ublinkage/transfer/backup/BackupHandlerTask.java

@@ -0,0 +1,61 @@
+package com.ublinkage.transfer.backup;
+
+import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.util.ZipUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Service;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.util.Date;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/**
+ * @author Yang Huang
+ * @create 2022-11-30-18:24
+ * @description 备份任务
+ */
+@Slf4j
+@Service
+public class BackupHandlerTask {
+    /**
+     * 备份天数
+     */
+    @Value("${send.backup-day}")
+    private String backupDay;
+    /**
+     * 备份路径
+     */
+    @Value("${send.backup-path}")
+    private String backupPath;
+
+    /**
+     * 每天凌晨1点检查过期文件
+     */
+    @Scheduled(cron = "0 0 1 * * ? ")
+    public void handlerBackup() {
+        try (Stream<Path> paths = Files.walk(Paths.get(backupPath))) {
+            List<Path> fileNames = paths.filter(Files::isRegularFile).collect(Collectors.toList());
+            for (Path file : fileNames) {
+                LocalDateTime startTime = FileUtil.lastModifiedTime(file.toFile()).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
+                LocalDateTime endTime = LocalDateTime.now();
+                if (startTime.isAfter(endTime)) {
+                    FileUtil.del(file.toFile());
+                }
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+            log.error("获取备份文件列表出现异常{}", e.getMessage());
+        }
+    }
+
+}

+ 3 - 1
tongfei_river_data_transfer/src/main/java/com/ublinkage/transfer/receive/HttpReceiveFile.java

@@ -35,12 +35,14 @@ public class HttpReceiveFile {
             inputStream = file.getInputStream();
             //这里可以改路径
             File receive = new File(receiveFilePath);
-            if (receive.exists()) {
+            if (!receive.exists()) {
                 boolean mkdirs = receive.mkdirs();
+                log.info("创建目录{},结果:{}", receiveFilePath, mkdirs);
             }
             fileOut = new FileOutputStream(receiveFilePath + fileName);
             IOUtils.copy(inputStream, fileOut);
             fileOut.flush();
+            log.info("接收文件{},成功。", fileOut);
         } catch (IOException e) {
             e.printStackTrace();
             return "error";

+ 31 - 13
tongfei_river_data_transfer/src/main/java/com/ublinkage/transfer/send/SendDataHandlerTask.java

@@ -38,11 +38,10 @@ public class SendDataHandlerTask {
 
     @Value("${send.file-path}")
     private String sendFilePath;
-    @Value("${send.to-ip}")
+    @Value("${send.to-addr}")
     private String sendToIp;
     @Value("${send.need-backup}")
     private boolean needBackup;
-
     @Value("${send.enable}")
     private boolean sendEnable;
     @Value("${send.backup-path}")
@@ -54,14 +53,14 @@ public class SendDataHandlerTask {
     /**
      * 处理接收到的数据,每3分钟处理一次
      */
-    @Scheduled(cron = "0 0/3 * * * ? ")
+    @Scheduled(cron = "0 0/1 * * * ? ")
     public void handlerReceiveData() {
         //启动压缩
         if (compressEnable) {
             for (String path : compressFilePath.split(",")) {
                 File file = new File(path);
                 if (!file.exists()) {
-                    boolean mkdirs = file.mkdirs();
+                    file.mkdirs();
                 }
                 // 先解压
                 try (Stream<Path> paths = Files.walk(Paths.get(path))) {
@@ -74,11 +73,14 @@ public class SendDataHandlerTask {
                             log.info("压缩文件:{}", zip);
                             //删除zip
                             FileUtil.del(fileName.toFile());
+                        } else {
+                            //如果不是zip就复制到目标目录
+                            FileUtil.move(fileName.toFile(), new File(compressZipPath + FileUtil.getName(fileName.toFile())), true);
                         }
                     }
                 } catch (IOException e) {
                     e.printStackTrace();
-                    log.error("处理出现异常{}", e.getMessage());
+                    log.error("获取压缩文件列表出现异常{}", e.getMessage());
                 }
             }
         }
@@ -86,6 +88,7 @@ public class SendDataHandlerTask {
         if (sendEnable) {
             //检查网络,如果网络是通常就发送到接收端,如果没有网络就只打包。
             if (!this.serverIsOnline()) {
+                log.error("请求网络{}异常.", sendToIp);
                 return;
             }
             try (Stream<Path> paths = Files.walk(Paths.get(sendFilePath))) {
@@ -93,14 +96,29 @@ public class SendDataHandlerTask {
                 log.info("发现有{}个待处理文件。", list.size());
                 for (Path filePath : list) {
                     try {
-                        httpSendFile.sendFile(filePath.toFile());
-                        if (needBackup) {
-                            //移动到目标目录
-                            File backupFile = new File(backupPath);
-                            if (!backupFile.exists()) {
-                                boolean mkdirs = backupFile.mkdirs();
+                        boolean result = httpSendFile.sendFile(filePath.toFile());
+                        if (result) {
+                            if (needBackup) {
+                                //移动到目标目录
+                                File backupFile = new File(backupPath);
+                                if (!backupFile.exists()) {
+                                    backupFile.mkdirs();
+                                }
+                                FileUtil.move(filePath.toFile(), new File(backupPath + FileUtil.getName(filePath)), true);
+                            } else {
+                                //如果不备份就删除
+                                FileUtil.del(filePath);
+                            }
+                        } else {
+                            if (needBackup) {
+                                String errorPath = backupPath + "error" + File.pathSeparator;
+                                //移动到目标目录
+                                File backupFile = new File(errorPath);
+                                if (!backupFile.exists()) {
+                                    backupFile.mkdirs();
+                                }
+                                FileUtil.move(filePath.toFile(), new File(errorPath + FileUtil.getName(filePath)), true);
                             }
-                            FileUtil.move(filePath.toFile(), new File(backupPath + FileUtil.getName(filePath)), true);
                         }
                     } catch (Exception e) {
                         e.printStackTrace();
@@ -122,7 +140,7 @@ public class SendDataHandlerTask {
      */
     private boolean serverIsOnline() {
         try {
-            String res = HttpUtil.get("http://" + sendToIp + ":9002", 2000);
+            String res = HttpUtil.get("http://" + sendToIp, 2000);
             return "OK".equals(res);
         } catch (Exception e) {
             return false;

+ 7 - 3
tongfei_river_data_transfer/src/main/java/com/ublinkage/transfer/util/HttpSendFile.java

@@ -25,7 +25,7 @@ import java.nio.charset.StandardCharsets;
 @Component
 public class HttpSendFile {
 
-    @Value("${send.to-ip}")
+    @Value("${send.to-addr}")
     private String sendToIp;
 
     /**
@@ -37,7 +37,7 @@ public class HttpSendFile {
     public boolean sendFile(File file) {
         CloseableHttpClient httpclient = HttpClients.createDefault();
         try {
-            HttpPost httpPost = new HttpPost("http://" + sendToIp + ":9002" + "/uploadFile");
+            HttpPost httpPost = new HttpPost("http://" + sendToIp + "/uploadFile");
             //创建接口需要的参数
             MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
             ContentType fileType = ContentType.create("application/octet-stream", StandardCharsets.UTF_8);
@@ -45,6 +45,7 @@ public class HttpSendFile {
 
             HttpEntity entity = entityBuilder.build();
             httpPost.setEntity(entity);
+            log.info("开始-发送文件{},到{}", file, sendToIp);
 
             //调用跨网文件发送接口
             HttpResponse response = httpclient.execute(httpPost);
@@ -52,7 +53,11 @@ public class HttpSendFile {
             HttpEntity responseEntity = response.getEntity();
             if (responseEntity != null) {
                 String result = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
+                log.info("结束-发送文件{},到{},响应状态:{}", file, sendToIp, result);
                 return "ok".equals(result);
+            } else {
+                log.info("结束-发送文件{},到{},响应状态:{}", file, sendToIp, "responseEntity为空。");
+                return false;
             }
         } catch (Exception e) {
             e.printStackTrace();
@@ -65,6 +70,5 @@ public class HttpSendFile {
                 e.printStackTrace();
             }
         }
-        return false;
     }
 }

+ 2 - 2
tongfei_river_data_transfer/src/main/resources/application.yml

@@ -1,5 +1,5 @@
 server:
-  port: 9002
+  port: 4000
 
 
 #-------发送端的验证
@@ -17,7 +17,7 @@ send:
   #是否启用发送功能
   enable: true
   #目标IP地址
-  to-ip: 192.168.1.6
+  to-addr: 10.165.7.1:4000
   file-path: /Users/ocean/Downloads/qxyb/
   #发送成功后需要备份到备份目录
   need-backup: true

+ 24 - 0
tongfei_river_data_transfer/src/test/java/com/ublinkage/transfer/SimpleTests.java

@@ -0,0 +1,24 @@
+package com.ublinkage.transfer;
+
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.io.FileUtil;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import java.util.Date;
+
+public class SimpleTests {
+
+
+    @Test
+    public void test1() {
+        Date date = FileUtil.lastModifiedTime("/Users/ocean/Downloads/send/GPM,20221129_2330.zip");
+        String format = DateUtil.format(date, "yyyy-MM-dd-HH:mm:ss");
+        System.out.println("打印输出...format的值:" + format);
+
+
+    }
+
+
+}