当前位置: 首页 > news >正文

在java 中如何实现执行linux命令,通过post接口代理出来?

接口方式输入命令得返回结果

    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();}

效果
在这里插入图片描述


http://www.mrgr.cn/news/49942.html

相关文章:

  • 【Hadoop】HDFS基本操作
  • JavaScript 常用方法
  • 牛客网上最全的Java八股文整理,涵盖Java全栈技术点
  • 如何下载3GPP协议?
  • 智慧交通:科技保障出行安全
  • 智慧灌区信息化管理系统解决方案
  • Libevent源码剖析-开篇
  • ffmpeg截取一段视频中一段视频
  • 从源码到平台:使用视频美颜SDK构建高性能直播美颜系统详解
  • 怎么把wma格式转换成mp3?介绍几种wma转换成MP3的转换方法
  • 麦沃德提升会议效率与质量:一款全面的会议管理系统
  • RuntimeError: Unable to find a valid cuDNN algorithm to run convolution
  • Redis日常运维
  • 前后分离项目记录
  • FrameWork使用EfCore数据库映射举例
  • 【live2d】看板娘人物模型使用方法
  • CASA(Carnegie-Ames-Stanford Approach) 模型原理及实践技术
  • 【Linux】解析信号的本质&相关函数及指令的介绍
  • 嵌入式归一板,传统归一板。你会如何选择?
  • 并行 parallel broadcast partition pruning 分区裁剪 optimizer_dynamic_sampling=7