Quellcode durchsuchen

增加系统命令调用工具方法

dsx vor 1 Jahr
Ursprung
Commit
a2cefbaaa9

+ 99 - 0
4dkankan-common-utils/src/main/java/com/fdkankan/common/util/CmdUtils.java

@@ -0,0 +1,99 @@
+package com.fdkankan.common.util;
+
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * Created by Xiewj on 2021/1/4 0004 14:53
+ */
+@Slf4j
+public class CmdUtils {
+
+
+    /**
+     * 调用算法 xx.sh 脚本
+     * @param command
+     */
+//    public static void callshell(String command){
+//        try {
+//            String[] cmd = new String[]{"/bin/sh", "-c", command};
+//            Process process = Runtime.getRuntime().exec(cmd);
+//            StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR");
+//            errorGobbler.start();
+//            StreamGobbler outGobbler = new StreamGobbler(process.getInputStream(), "STDOUT");
+//            outGobbler.start();
+//            process.waitFor();
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//        }
+//    }
+
+
+
+    public static void callLineSh(String command) throws Exception {
+        callLineSh(command, null);
+
+    }
+
+    /**
+     * 直接java调用命令
+     * @param command
+     */
+    public static void callLine(String command) throws Exception {
+        callLine(command, null);
+
+    }
+
+    /**
+     * 直接java调用命令
+     * @param command
+     */
+    public static void callLine(String command, Integer lineSize) throws Exception {
+        log.info("cmd: " + command);
+        Process process = Runtime.getRuntime().exec(command);
+        log.info("开始运行");
+        StreamGobblerLine errorGobbler = new StreamGobblerLine(process.getErrorStream(), "ERROR");
+        errorGobbler.start();
+        // 200行打印一次日志
+        StreamGobblerLine outGobbler = new StreamGobblerLine(process.getInputStream(), "STDOUT", lineSize);
+        outGobbler.start();
+        process.waitFor();
+    }
+
+    /**
+     *
+     * @param command 命令
+     * @param lineSize 日志输出行数 ,可以为null
+     */
+    public static void callLineSh(String command, Integer lineSize) throws Exception {
+        log.info("cmd: " + command);
+        String[] cmd = new String[]{"/bin/sh", "-c", command};
+        Process process = Runtime.getRuntime().exec(cmd);
+        log.info("开始运行");
+        StreamGobblerLine errorGobbler = new StreamGobblerLine(process.getErrorStream(), "ERROR");
+        errorGobbler.start();
+        // 200行打印一次日志
+        StreamGobblerLine outGobbler = new StreamGobblerLine(process.getInputStream(), "STDOUT", lineSize);
+        outGobbler.start();
+        process.waitFor();
+    }
+
+    /**
+     * 调用sh脚本上传oss
+     */
+//    public static void ossUploadDir(String sceneCode, String uploadDir, String target){
+//
+//        String cmd = CmdConstant.OSSUTIL_UPLOAD_DIR;
+//        cmd = cmd.replaceAll("@sceneCode", sceneCode);
+//        cmd = cmd.replaceAll("@uploadDir", uploadDir);
+//        cmd = cmd.replaceAll("@target", target);
+//
+//        log.info("ossCmd: " + cmd);
+//        long start = System.currentTimeMillis();
+//        CmdUtils.callLineSh(cmd);
+//        long end = System.currentTimeMillis();
+//        log.info("场景码目录:{} 上传完成, 耗时:{} s" , sceneCode, (end-start)/1000 );
+//    }
+
+
+
+}

+ 79 - 0
4dkankan-common-utils/src/main/java/com/fdkankan/common/util/StreamGobblerLine.java

@@ -0,0 +1,79 @@
+package com.fdkankan.common.util;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.*;
+
+@Slf4j
+public class StreamGobblerLine extends Thread {
+
+	InputStream is;
+    String type;
+    OutputStream os;
+    Integer lineSize;  // 多少行打印日志一次
+
+    public StreamGobblerLine(InputStream is, String type) {
+        this(is, type, null, null);
+    }
+
+    public StreamGobblerLine(InputStream is, String type, Integer lineSize) {
+        this(is, type, null, lineSize);
+    }
+
+    StreamGobblerLine(InputStream is, String type, OutputStream redirect, Integer lineSize) {
+        this.is = is;
+        this.type = type;
+        this.os = redirect;
+        this.lineSize = lineSize;
+    }
+
+    public void run() {
+        log.info("run StreamGobblerLine");
+
+        InputStreamReader isr = null;
+        BufferedReader br = null;
+        PrintWriter pw = null;
+        try {
+            if (os != null)
+                pw = new PrintWriter(os);
+
+            isr = new InputStreamReader(is);
+            br = new BufferedReader(isr);
+            String line=null;
+            int i = 1;
+            while ( (line = br.readLine()) != null) {
+                if (lineSize != null) {
+                    if (i % lineSize == 0) {
+                        log.info(type + "," + i +" : >" + line);
+                    }
+                } else {
+                    log.info(type + ","  + i +" : >" + line);
+                }
+                i++;
+
+            }
+
+            if (pw != null)
+                pw.flush();
+        } catch (IOException ioe) {
+            ioe.printStackTrace();
+        } finally{
+            try {
+            	if(pw!=null)
+            	{
+            		 pw.close();
+            	}
+            	if(br!=null)
+            	{
+            		br.close();
+            	}
+            	if(isr!=null)
+            	{
+            		isr.close();
+            	}
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+}