|
@@ -1,192 +0,0 @@
|
|
|
-package com.fd.util;
|
|
|
-
|
|
|
-import lombok.extern.log4j.Log4j2;
|
|
|
-
|
|
|
-import java.io.BufferedReader;
|
|
|
-import java.io.InputStreamReader;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Arrays;
|
|
|
-
|
|
|
-/**
|
|
|
- * Created by Owen on 2019/10/31 0031 16:05
|
|
|
- */
|
|
|
-@Log4j2
|
|
|
-public class CmdUtils {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- // linux 执行方法
|
|
|
- public static String exeCmd(String commandStr) {
|
|
|
-
|
|
|
- String result = null;
|
|
|
- try {
|
|
|
- String[] cmd = new String[]{"/bin/sh", "-c",commandStr};
|
|
|
- Process ps = Runtime.getRuntime().exec(cmd);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream(),"UTF-8"));
|
|
|
- BufferedReader errorBuf = new BufferedReader(new InputStreamReader(ps.getErrorStream(), "UTF-8"));
|
|
|
- log.info("===== br ========: {}", br == null);
|
|
|
- StringBuffer sb = new StringBuffer();
|
|
|
- StringBuffer errorStr = new StringBuffer();
|
|
|
- String line;
|
|
|
-
|
|
|
- while ((line = errorBuf.readLine()) != null) {
|
|
|
- errorStr.append(line).append("\n");
|
|
|
- System.out.println("error: "+line);
|
|
|
- }
|
|
|
- log.info("error result: {}", errorStr.toString());
|
|
|
-
|
|
|
-
|
|
|
- while ((line = br.readLine()) != null) {
|
|
|
- log.info("===== br.readLine: ======== {}", br.readLine());
|
|
|
- //执行结果加上回车
|
|
|
- System.out.println("br: "+line);
|
|
|
- sb.append(line).append("\n");
|
|
|
- log.info("line: {}", line);
|
|
|
- }
|
|
|
- result = sb.toString();
|
|
|
- System.out.println("result: " + result);
|
|
|
- log.info("result: {}", result);
|
|
|
-
|
|
|
- // 结束命令行
|
|
|
- int i = ps.waitFor();
|
|
|
- log.info("wsitFore : {}", i);
|
|
|
-
|
|
|
- // 关闭流
|
|
|
- br.close();
|
|
|
- errorBuf.close();
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
- return result;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- // linux 执行方法
|
|
|
- public static String exeCmd2(String commandStr) {
|
|
|
-
|
|
|
- String result = null;
|
|
|
- try {
|
|
|
- String[] cmd = new String[]{"/bin/sh", "-c",commandStr};
|
|
|
- Process ps = Runtime.getRuntime().exec(cmd);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream(),"UTF-8"));
|
|
|
- BufferedReader errorBuf = new BufferedReader(new InputStreamReader(ps.getErrorStream(), "UTF-8"));
|
|
|
-
|
|
|
- StringBuffer sb = new StringBuffer();
|
|
|
- StringBuffer errorStr = new StringBuffer();
|
|
|
-
|
|
|
-
|
|
|
- String errorLine;
|
|
|
-
|
|
|
- String str = "";
|
|
|
- String b= "";
|
|
|
-
|
|
|
-// StringBuffer per = new StringBuffer();
|
|
|
- ArrayList<Object> arrayList = new ArrayList<>();
|
|
|
-
|
|
|
- while ((errorLine = errorBuf.readLine()) != null) {
|
|
|
-// errorStr.append("\n"); // 为了打印日志好看,第一行先换行
|
|
|
- errorStr.append(errorLine).append("\n");
|
|
|
-
|
|
|
- if (errorLine.contains("Generated tile #")) {
|
|
|
- // 截取百分比
|
|
|
- str = errorLine.substring(errorLine.indexOf("% done)") - 7, errorLine.indexOf("% done)"));
|
|
|
- arrayList.add(str);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-// b = errorLine.substring(0, errorLine.lastIndexOf("All done. {")+8);
|
|
|
-//
|
|
|
-// if ("All done".equals(b)) {
|
|
|
-// arrayList.add(b);
|
|
|
-// }
|
|
|
- }
|
|
|
- log.info("error result: {}", errorStr.toString());
|
|
|
- log.info("per: {}", Arrays.toString(arrayList.toArray()));
|
|
|
-
|
|
|
- String line;
|
|
|
- while ((line = br.readLine()) != null) {
|
|
|
- log.info("===== br.readLine: ======== {}", br.readLine());
|
|
|
- //执行结果加上回车
|
|
|
- System.out.println("br: "+line);
|
|
|
- sb.append(line).append("\n");
|
|
|
- log.info("line: {}", line);
|
|
|
- }
|
|
|
- result = sb.toString();
|
|
|
- System.out.println("result: " + result);
|
|
|
- log.info("result: {}", result);
|
|
|
-
|
|
|
- // 结束命令行
|
|
|
- int i = ps.waitFor();
|
|
|
- log.info("wsitFore : {}", i);
|
|
|
-
|
|
|
- // 关闭流
|
|
|
- br.close();
|
|
|
- errorBuf.close();
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
- return result;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- // 记录进度
|
|
|
- public class PercentThread implements Runnable{
|
|
|
-
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static void main(String[] args) {
|
|
|
-// StringBuffer buffer = new StringBuffer();
|
|
|
-//
|
|
|
-// for (int i = 0; i < 10; i++) {
|
|
|
-// buffer.append("ni hao:" + i).append("\n");
|
|
|
-// }
|
|
|
-// System.out.println(buffer.toString());
|
|
|
-
|
|
|
- String a = "Generated tile #470 of 470 ( 7.95 % done): 23-3419770-1832098 (pseudomerc, extents: 12637123.684560,2532482.957259:12637133.239189,2532492.511887) [mesh,atlas]. {encoder.cpp:process():304}";
|
|
|
- String b = "All done. {";
|
|
|
- System.out.println(a.substring(a.indexOf("% done)")-7, a.indexOf("% done)")));
|
|
|
- System.out.println(b.substring(0, b.lastIndexOf("All done. {")+8));
|
|
|
-
|
|
|
- System.out.println(a.contentEquals("Generated tile #"));
|
|
|
- System.out.println(a.contains("Generated tile #"));
|
|
|
- System.out.println(a.concat("Generated tile #"));
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-// // linux 执行方法
|
|
|
-// public static String exeCmd(String commandStr) {
|
|
|
-//
|
|
|
-// String result = null;
|
|
|
-// try {
|
|
|
-// String[] cmd = new String[]{"/bin/sh", "-c",commandStr};
|
|
|
-// Process ps = Runtime.getRuntime().exec(cmd);
|
|
|
-//
|
|
|
-// BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
|
|
|
-// log.info("===== br ========: {}", br == null);
|
|
|
-// StringBuffer sb = new StringBuffer();
|
|
|
-// log.info("===== sb ========");
|
|
|
-//
|
|
|
-//
|
|
|
-// } catch (Exception e) {
|
|
|
-// e.printStackTrace();
|
|
|
-// }
|
|
|
-//
|
|
|
-// return result;
|
|
|
-//
|
|
|
-// }
|
|
|
-}
|