Forráskód Böngészése

1.添加文件下载及scp移动文件到功率预测服务器

wangtao 3 éve
szülő
commit
ba8d1b29ec

+ 10 - 0
client.yml

@@ -0,0 +1,10 @@
+#青海client小程序使用jar包外配置文件
+remote:
+  ip: 192.168.1.202
+  port: 22
+  user: root
+  pwd: Syjy*3377
+  path: /home/syjy/ipfcstV3/downLoadFile/
+v3cloud:
+  filelogUrl: https://117.78.19.70:9010/client/getDownLoadFile/
+  fileDownUrl: https://117.78.19.70:9010/client/getFileById?id=

+ 59 - 0
ipfcst-client/src/main/java/com/jiayue/client/entity/FileCreateLog.java

@@ -0,0 +1,59 @@
+package com.jiayue.client.entity;
+
+import lombok.Data;
+
+/**
+ * 文件生成列表
+ *
+ * @author bizy
+ * @version 3.0
+ */
+@Data
+public class FileCreateLog {
+    private String id;
+
+    /**
+     * 场站编码
+     */
+    private String stationCode;
+
+    /**
+     * 场站名称
+     */
+    private String stationName;
+
+    /**
+     * 文件名
+     */
+    private String fileName;
+
+    /**
+     * 文件存放路径
+     */
+    private String filePath;
+
+    /**
+     * 日期
+     */
+    private String fileDate;
+
+    /**
+     * 是否下载
+     */
+    private String fileDownLoadStatus;
+
+    /**
+     * 是否传入内网
+     */
+    private String fileInStatus;
+
+    /**
+     * 下载到外网的方式
+     */
+    private String fileDownLoadType;
+
+    /**
+     * 文件类型
+     */
+    private String fileType;
+}

+ 98 - 0
ipfcst-client/src/main/java/com/jiayue/client/job/DownloadFileJob.java

@@ -0,0 +1,98 @@
+package com.jiayue.client.job;
+
+import cn.hutool.core.convert.Convert;
+import cn.hutool.http.HttpRequest;
+import cn.hutool.http.HttpUtil;
+import cn.hutool.json.JSONArray;
+import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
+import com.jiayue.client.entity.FileCreateLog;
+import com.jiayue.client.service.QueryDataService;
+import com.jiayue.client.util.FileUtil;
+import com.jiayue.client.util.Scpclient;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
+import org.springframework.core.io.PathResource;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Service;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * 青海集中功率预测从云平台下载气象文件数据:
+ * 1.每天凌晨5点执行,下载当天气象文件传给功率预测服务器
+ */
+@Service
+@Slf4j
+public class DownloadFileJob {
+
+    @Autowired
+    public QueryDataService queryDataService;
+    //获取下载到文件,从临时目录移动到目标目录
+    public static List<String> filedownLoadList = new ArrayList<String>();
+    public static Properties properties = new Properties();
+
+    static {
+        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
+        yaml.setResources(new PathResource("./client.yml"));
+        properties = yaml.getObject();
+    }
+
+    /**
+     * 每天凌晨2点执行
+     */
+    @Scheduled(cron = "0 09 10 * * ?")
+    public void download() {
+        log.info("上午文件下载定时任务开始");
+        List<Map<String, Object>> stations = queryDataService.getStations();
+        for (Map<String, Object> station : stations) {
+            String stationCode = Convert.toStr(station.get("C_STATION_CODE"));
+            log.info("开始执行定时任务:http下载场站:{} 的文件。", stationCode);
+            String body = HttpRequest.get(properties.getProperty("v3cloud.filelogUrl") + stationCode).execute().body();
+            JSONObject json = JSONUtil.parseObj(body);
+            String code = json.get("code").toString();
+            String data = json.get("data").toString();
+            if (code.equals("0") && data.length() > 0) {
+                JSONArray array = JSONUtil.parseArray(data);
+                List<FileCreateLog> list = array.toList(FileCreateLog.class);
+                String path = FileUtil.judeDirExists("/Users/wangtao/Documents/temp/client");
+
+                try {
+                    for (FileCreateLog fileCreateLog : list) {
+                        String url = properties.getProperty("v3cloud.fileDownUrl") + fileCreateLog.getId();// "https://117.78.19.70:9010/client/getFileById?id=" + fileCreateLog.getId();
+                        Long size = HttpUtil.downloadFile(url, path + File.separatorChar + fileCreateLog.getFileName());
+                        if (size > 0) {
+                            log.info("成功下载文件:{}", path + File.separatorChar + fileCreateLog.getFileName());
+                            filedownLoadList.add(path + File.separatorChar + fileCreateLog.getFileName());
+                        }
+                    }
+                } catch (Exception e) {
+                    log.error("文件下载失败:{}", e);
+                } finally {
+                    //把下载的文件挪到功率预测服务器文件夹
+                    if (filedownLoadList.size() > 0) {
+                        for (String fpath : filedownLoadList) {
+                            try {
+                                Scpclient scpclient = Scpclient.getInstance(properties.getProperty("remote.ip"),
+                                        Convert.toInt(properties.getProperty("remote.port")), properties.getProperty("remote.user"),
+                                        properties.getProperty("remote.pwd"));
+
+                                scpclient.putFile(fpath, properties.getProperty("remote.path") + stationCode + "/new/");
+                            } catch (Exception e) {
+                                log.error("移动文件到功率预测服务器失败:{}", e);
+                            }
+                        }
+                    }
+                }
+            }
+        }
+
+    }
+
+
+}

+ 62 - 0
ipfcst-client/src/main/java/com/jiayue/client/util/FileUtil.java

@@ -0,0 +1,62 @@
+package com.jiayue.client.util;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.File;
+
+@Slf4j
+public class FileUtil {
+    public static void mvFile(String fsPath, String fdPath) {
+//        File fs = new File("D:\\迅雷下载\\f31.mp4");
+//        File fd = new File("D:\\");
+        //源文件
+        File fs = new File(fsPath);
+        //目标目录
+        File fd = new File(fdPath);
+        //判断目标目录是否存在同名文件,存在则删除
+        File md = new File(fdPath + fs.getName());
+        if (md.exists()) {
+            md.delete();
+        }
+        try {
+            fs.renameTo(fd);
+        } catch (Exception e) {
+            log.error("文件移动错误", e);
+        }
+    }
+
+    public static String judeDirExists(String path) {
+        try {
+
+            path = path.replaceAll("\\\\", "/");
+            path = "/" + path;
+
+
+            File file = new File(path);
+
+            if (file.exists()) {
+                if (file.isDirectory()) {
+                    log.debug("dir exists");
+                } else {
+                    log.debug("the same name file exists, can not create dir");
+                }
+            } else {
+                log.debug("dir not exists, create it ...");
+                File parent = file.getParentFile(); // 获取父文件
+                if (!parent.exists()) {
+                    parent.mkdirs(); //创建所有父文件夹
+                }
+                file.mkdir();
+
+                /*if (osName.indexOf("Windows") <= -1) {
+                    Runtime.getRuntime().exec("chmod 777 -R " + path);//ftp服务器在linux环境中,生成的文件涉及到读写权限问题,直接给777权限
+                }*/
+            }
+            log.info("path:" + path);
+        } catch (Exception e) {
+            log.error("系统错误:" + e.getMessage(), e);
+        } finally {
+            return path;
+        }
+    }
+}

+ 145 - 0
ipfcst-client/src/main/java/com/jiayue/client/util/Scpclient.java

@@ -0,0 +1,145 @@
+package com.jiayue.client.util;
+
+import ch.ethz.ssh2.Connection;
+import ch.ethz.ssh2.SCPClient;
+import ch.ethz.ssh2.Session;
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Map;
+
+@Slf4j
+public class Scpclient {
+    static private Scpclient instance;
+
+    static synchronized public Scpclient getInstance(String IP, int port,
+                                                     String username, String passward) {
+        if (instance == null) {
+            instance = new Scpclient(IP, port, username, passward);
+        }
+        return instance;
+    }
+
+    public Scpclient(String IP, int port, String username, String passward) {
+        this.ip = IP;
+        this.port = port;
+        this.username = username;
+        this.password = passward;
+    }
+
+    /**
+     * 远程拷贝文件
+     *
+     * @param remoteFile           远程源文件路径
+     * @param localTargetDirectory 本地存放文件路径
+     */
+    public Map<String, Object> getFile(String remoteFile, String localTargetDirectory) {
+        Connection conn = new Connection(ip, port);
+        try {
+            conn.connect();
+            boolean isAuthenticated = conn.authenticateWithPassword(username,
+                    password);
+            if (isAuthenticated == false) {
+                log.info("authentication failed");
+                return null;
+            }
+            SCPClient client = new SCPClient(conn);
+            client.get(remoteFile, localTargetDirectory);
+            conn.close();
+        } catch (IOException e) {
+            log.info(e.getMessage());
+            return null;
+        }
+        return null;
+    }
+
+
+    /**
+     * 远程上传文件
+     *
+     * @param localFile             本地文件路径
+     * @param remoteTargetDirectory 远程存放文件路径
+     */
+    public void putFile(String localFile, String remoteTargetDirectory) {
+        Connection conn = new Connection(ip, port);
+        try {
+            conn.connect();
+            boolean isAuthenticated = conn.authenticateWithPassword(username,
+                    password);
+            if (isAuthenticated == false) {
+                log.info("authentication failed");
+            }
+            SCPClient client = new SCPClient(conn);
+            client.put(localFile, remoteTargetDirectory);
+            conn.close();
+            log.info("移动文件:{} 到 {} 成功", localFile, remoteTargetDirectory);
+        } catch (IOException e) {
+            log.info(e.getMessage());
+        }
+    }
+
+    /**
+     * 远程上传文件并对上传文件重命名
+     *
+     * @param localFile             本地文件路径
+     * @param remoteFileName        远程文件名
+     * @param remoteTargetDirectory 远程存放文件路径
+     * @param mode                  默认"0600",length=4
+     */
+    public void putFile(String localFile, String remoteFileName, String remoteTargetDirectory, String mode) {
+        Connection conn = new Connection(ip, port);
+        try {
+            conn.connect();
+            boolean isAuthenticated = conn.authenticateWithPassword(username,
+                    password);
+            if (isAuthenticated == false) {
+                log.info("authentication failed");
+            }
+            SCPClient client = new SCPClient(conn);
+            if ((mode == null) || (mode.length() == 0)) {
+                mode = "0600";
+            }
+            client.put(localFile, remoteFileName, remoteTargetDirectory, mode);
+
+            //重命名
+            Session sess = conn.openSession();
+            String tmpPathName = remoteTargetDirectory + File.separator + remoteFileName;
+            String newPathName = tmpPathName.substring(0, tmpPathName.lastIndexOf("."));
+            sess.execCommand("mv " + remoteFileName + " " + newPathName);
+
+            conn.close();
+        } catch (IOException e) {
+            log.info(e.getMessage());
+        }
+    }
+
+    private String ip;
+    private int port;
+    private String username;
+    private String password;
+
+    public String getUsername() {
+        return username;
+    }
+
+    public void setUsername(String username) {
+        this.username = username;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    public int getPort() {
+        return port;
+    }
+
+    public void setPort(int port) {
+        this.port = port;
+    }
+}

+ 63 - 0
ipfcst-client/src/test/java/com/jiayue/client/job/DownloadFileJobTest.java

@@ -0,0 +1,63 @@
+package com.jiayue.client.job;
+
+import cn.hutool.http.HttpRequest;
+import cn.hutool.http.HttpUtil;
+import cn.hutool.json.JSONArray;
+import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
+import com.jiayue.client.entity.FileCreateLog;
+import com.jiayue.client.util.FileUtil;
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+@Slf4j
+public class DownloadFileJobTest {
+
+    public static List<String> fileList = new ArrayList<String>();
+    //获取下载到文件,从临时目录移动到目标目录
+    public static List<String> filedownLoadList = new ArrayList<String>();
+   
+
+    public static void main(String[] args) {
+        String stationCode = "T00003";
+        log.info("开始执行定时任务:http下载场站:{} 的文件。", stationCode);
+        String body = HttpRequest.get("https://117.78.19.70:9010/client/getDownLoadFile/" + stationCode).execute().body();
+        JSONObject json = JSONUtil.parseObj(body);
+        String code = json.get("code").toString();
+        String data = json.get("data").toString();
+        if (code.equals("0") && data.length() > 0) {
+            JSONArray array = JSONUtil.parseArray(data);
+            List<FileCreateLog> list = array.toList(FileCreateLog.class);
+            String path = FileUtil.judeDirExists("/Users/wangtao/Documents/temp/client");
+
+            try {
+                for (FileCreateLog fileCreateLog : list) {
+                    String url = "https://117.78.19.70:9010/client/getFileById?id=" + fileCreateLog.getId();
+                    Long size = HttpUtil.downloadFile(url, path + File.separatorChar + fileCreateLog.getFileName());
+                    if (size > 0) {
+                        log.info("成功下载文件:{}", path + File.separatorChar + fileCreateLog.getFileName());
+                        filedownLoadList.add(path + File.separatorChar + fileCreateLog.getFileName());
+                    }
+                }
+            } catch (Exception e) {
+                log.error("文件下载失败:{}", e);
+            } finally {
+                //把下载的文件挪到功率预测服务器文件夹
+                if (filedownLoadList.size() > 0) {
+                    for (String fpath : filedownLoadList) {
+                        try {
+                            //TODO scp 命令发文件
+                            Runtime.getRuntime().exec("chmod 777 -R " + path);
+                        } catch (IOException e) {
+                            log.error("移动文件到功率预测服务器失败:{}", e);
+                        }
+                    }
+                }
+            }
+        }
+    }
+}

+ 24 - 0
ipfcst-client/src/test/java/com/jiayue/client/util/ScpclientTest.java

@@ -0,0 +1,24 @@
+package com.jiayue.client.util;
+
+import cn.hutool.core.convert.Convert;
+import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
+import org.springframework.core.io.PathResource;
+
+import java.util.Properties;
+
+class ScpclientTest {
+    public static Properties properties = new Properties();
+
+    static {
+        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
+        yaml.setResources(new PathResource("./client.yml"));
+        properties = yaml.getObject();
+    }
+
+    public static void main(String[] args) {
+        Scpclient scpclient = Scpclient.getInstance(properties.getProperty("remote.ip"),
+                Convert.toInt(properties.getProperty("remote.port")), properties.getProperty("remote.user"),
+                properties.getProperty("remote.pwd"));
+        scpclient.putFile("/Users/wangtao/Documents/temp/client/DQ_202109270400480.RB", "/home/test/");
+    }
+}