فهرست منبع

fyun-parent sh init

lyhzzz 1 سال پیش
والد
کامیت
7037b302b5

+ 1 - 1
4dkankan-utils-fyun-parent/pom.xml

@@ -11,7 +11,7 @@
     <packaging>jar</packaging>
 
     <artifactId>4dkankan-utils-fyun-parent</artifactId>
-
+    <version>5.0.0-SNAPSHOT</version>
 
     <dependencies>
 

+ 8 - 0
4dkankan-utils-fyun-parent/src/main/java/com/fdkankan/fyun/constant/FileNameConstants.java

@@ -0,0 +1,8 @@
+package com.fdkankan.fyun.constant;
+
+import java.util.Arrays;
+import java.util.List;
+
+public class FileNameConstants {
+    public static List<String> fileNames = Arrays.asList("fyun-download.sh","fyun-upload.sh","oss.sh","ossutil64","sshoss.sh","upload.sh");
+}

+ 35 - 0
4dkankan-utils-fyun-parent/src/main/java/com/fdkankan/fyun/init/InitSh.java

@@ -0,0 +1,35 @@
+package com.fdkankan.fyun.init;
+
+import com.fdkankan.fyun.constant.FileNameConstants;
+import com.fdkankan.fyun.utils.ShellUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.io.File;
+
+@Component
+@Slf4j
+public class InitSh {
+
+    @Value("${fyun.shell.path:/opt/ossutil}")
+    public String shellPath;
+    @Value("${fyun.shell.script.path:https://4dkk.4dage.com/script/4dkankan_v4/fyun}")
+    public String ossPath;
+
+    @PostConstruct
+    public void checkSh(){
+        log.info("开始启动检查上传下载脚本");
+        if(!new File(shellPath).exists()){
+            new File(shellPath).mkdirs();
+        }
+        for ( String fileName :FileNameConstants.fileNames){
+            File file = new File(shellPath + "/" + fileName);
+            if(!file.exists()){
+                log.info("文件不存在,下载文件:{}",shellPath + "/" + fileName);
+                ShellUtil.execCmd(String.format(ShellUtil.wgetShell,shellPath+"/"+fileName,ossPath +"/"+fileName));
+            }
+        }
+    }
+}

+ 110 - 0
4dkankan-utils-fyun-parent/src/main/java/com/fdkankan/fyun/utils/ShellUtil.java

@@ -0,0 +1,110 @@
+package com.fdkankan.fyun.utils;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.BufferedReader;
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+@Slf4j
+public class ShellUtil {
+
+    public static String wgetShell = "wget -O %s %s";
+
+    /**
+     * 执行系统命令, 返回执行结果
+     * @param cmd 需要执行的命令
+     */
+    public static void execCmd(String cmd) {
+        StringBuilder result = new StringBuilder();
+
+        Process process = null;
+        BufferedReader bufrIn = null;
+        BufferedReader bufrError = null;
+        long startTime = System.currentTimeMillis();
+
+        try {
+            // 执行命令, 返回一个子进程对象(命令在子进程中执行)
+            log.info("执行cmd:{}",cmd);
+            process = Runtime.getRuntime().exec(cmd);
+            // 获取命令执行结果, 有两个结果: 正常的输出 和 错误的输出(PS: 子进程的输出就是主进程的输入)
+            //处理InputStream的线程
+            threadRun(process);
+            // 方法阻塞, 等待命令执行完成(成功会返回0)
+            process.waitFor();
+        }catch (Exception e){
+            e.printStackTrace();
+        }finally {
+            closeStream(bufrIn);
+            closeStream(bufrError);
+            // 销毁子进程
+            if (process != null) {
+                process.destroy();
+            }
+        }
+        // 返回执行结果
+        log.info("执行cmd:{},结果:{},耗时:{}", cmd,result.toString(),System.currentTimeMillis() -startTime);
+    }
+
+    private static void threadRun(Process process) {
+        new Thread() {
+            @Override
+            public void run() {
+                BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
+                String line = null;
+                try {
+                    while((line = in.readLine()) != null) {
+                       log.debug("output: " + line);
+                    }
+                }
+                catch (IOException e) {
+                    e.printStackTrace();
+                }
+                finally {
+                    try {
+                        in.close();
+                    }
+                    catch (IOException e) {
+                        e.printStackTrace();
+                    }
+                }
+            }
+        }.start();
+        new Thread() {
+            @Override
+            public void run() {
+                BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
+                String line = null;
+                try {
+                    while((line = err.readLine()) != null) {
+                        log.debug("err: " + line);
+                    }
+                }
+                catch (IOException e) {
+                    e.printStackTrace();
+                }
+                finally {
+                    try {
+                        err.close();
+                    }
+                    catch (IOException e) {
+                        e.printStackTrace();
+                    }
+                }
+            }
+        }.start();
+    }
+
+    private static void closeStream(Closeable stream) {
+        if (stream != null) {
+            try {
+                stream.close();
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+
+}