From 91cbdeebf9b7dc572c73a2953bdaa715510bcb47 Mon Sep 17 00:00:00 2001 From: duhui Date: Mon, 16 Aug 2021 18:02:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=81=9C=E6=9C=8D=E8=84=9A=E6=9C=AC=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E8=BE=93=E5=87=BA=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jmfy/utils/AutoServerManager.java | 39 +++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/jmfy/utils/AutoServerManager.java b/src/main/java/com/jmfy/utils/AutoServerManager.java index a40fe62..84045e5 100644 --- a/src/main/java/com/jmfy/utils/AutoServerManager.java +++ b/src/main/java/com/jmfy/utils/AutoServerManager.java @@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import javax.annotation.Resource; +import java.io.*; import java.util.List; import java.util.Optional; import java.util.stream.Stream; @@ -158,10 +159,8 @@ public class AutoServerManager { exec = Runtime.getRuntime().exec(command); LOGGER.info("========================== 停服脚本执行结果开始 ==========================="); - new DealProcessSream(exec.getInputStream(),true).start(); - new DealProcessSream(exec.getErrorStream(),false).start(); + readProcessOutput(exec); LOGGER.info("=========================== 停服脚本执行结果结束 =========================="); - exec.waitFor(); } catch (Exception e) { e.printStackTrace(); @@ -171,4 +170,38 @@ public class AutoServerManager { } } } + + /** + * 打印进程输出 + * @param process 进程 + */ + private static void readProcessOutput(final Process process) { + // 将进程的正常输出在 System.out 中打印,进程的错误输出在 System.err 中打印 + read(process.getInputStream(), System.out); + read(process.getErrorStream(), System.err); + + } + + /** + * 读取输入流 + * @param inputStream + * @param out + */ + private static void read(InputStream inputStream, PrintStream out) { + try { + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + String line; + while ((line = reader.readLine()) != null) { + out.println(line); + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } }