说明:项目必须是部署在Linux服务器中才能生效。
1. 工具类@Controller
public class ExecuteNewFlowUtil {
public List executeNewFlow(List commands) {
List rspList = new ArrayList();
Runtime run = Runtime.getRuntime();
try {
Process proc = run.exec("/bin/bash", null, null);
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
for (String line : commands) {
out.println(line);
}
out.println("exit");// 这个命令必须执行,否则in流不结束。
String rspLine = "";
while ((rspLine = in.readLine()) != null) {
System.out.println(rspLine);
rspList.add(rspLine);
}
proc.waitFor();
in.close();
out.close();
proc.destroy();
} catch (IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return rspList;
}
}
2.方法执行代码段
//运行命令生成对比信息 ListgenerateComparativeInformationCommands = new ArrayList<>(); generateComparativeInformationCommands.add("命令1"); generateComparativeInformationCommands.add("命令2"); generateComparativeInformationCommands.add("命令3"); executeNewFlowUtil.executeNewFlow(generateComparativeInformationCommands);



