接口方式输入命令得返回结果
public AjaxResult doPost(HttpServletRequest request, HttpServletResponse response, String command) throws ServletException, IOException {// 设置响应内容类型 text/plain
// response.setContentType("application/json");PrintWriter out = response.getWriter();try {// 要执行的命令(这里以"ls -l"为例)if(null == command){command = "ls -l";}// 执行命令并捕获输出Process process = Runtime.getRuntime().exec(command);BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));String line;while ((line = reader.readLine()) != null) {log.info("执行结果: " + line);out.println(line);out.flush();}// 等待进程完成并检查退出值int exitCode = process.waitFor();if (exitCode != 0) {out.println("Command exited with error code: " + exitCode);}} catch (Exception e) {e.printStackTrace(out);} finally {out.close();}return AjaxResult.success();}
效果