java服务端调用服务端本地的bat

/**
 * 执行一个cmd命令
 *
 * @param cmdCommand cmd命令
 * @return 命令执行结果字符串,如出现异常返回null
 */
public static String executeCmdCommand(String cmdCommand) {
   StringBuilder stringBuilder = new StringBuilder();
   Process process = null;
   try {
      process = Runtime.getRuntime().exec(cmdCommand);
      BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(process.getInputStream(), "GBK"));
      String line = null;
      while ((line = bufferedReader.readLine()) != null) {
         stringBuilder.append(line).append(" ");
      }
      return stringBuilder.toString();
   } catch (Exception e) {
      e.printStackTrace();
      return null;
   }
}

/**
 * 执行bat文件,
 *
 * @param file          bat文件路径
 * @param isCloseWindow 执行完毕后是否关闭cmd窗口
 * @return bat文件输出log
 */
public static String executeBatFile(String file, boolean isCloseWindow) {
   String cmdCommand = null;
   if (isCloseWindow) {
      cmdCommand = "cmd.exe /c " + file;
   } else {
      cmdCommand = "cmd.exe /k " + file;
   }
   StringBuilder stringBuilder = new StringBuilder();
   Process process = null;
   try {
      process = Runtime.getRuntime().exec(cmdCommand);
      BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(process.getInputStream(), "GBK"));
      String line = null;
      while ((line = bufferedReader.readLine()) != null) {
         stringBuilder.append(line).append(" ");
      }
      return stringBuilder.toString();
   } catch (Exception e) {
      e.printStackTrace();
      return null;
   }
}

/**
 * 远程调用
 */
public static void RemoteBatchCall () {
   try {
      // 构建批处理命令

      String command = "cmd /c start /wait a.bat";

      // 创建ProcessBuilder对象
      ProcessBuilder pb = new ProcessBuilder(command.split(" "));
      // 设置工作目录(可选)
      //pb.directory(new File("E:\\Seeyon\\NginxServer\\startup.bat"));
      pb.directory(new File("D:\\Siemens\\Teamcenter13\\portal\\portal.bat"));

      // 启动进程
      Process process = pb.start();

      // 获取命令输出
      BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
      String line;
      while ((line = reader.readLine()) != null) {
         System.out.println(line);
      }

      // 等待命令执行完毕
      int exitCode = process.waitFor();
      System.out.println("批处理命令执行完毕,退出码:" + exitCode);
   } catch (IOException | InterruptedException e) {
      e.printStackTrace();
   }
}


/**
 * 执行bat文件,新开窗口
 *
 * @param file          bat文件路径
 * @param isCloseWindow 执行完毕后是否关闭cmd窗口
 * @return bat文件输出log
 */
public static String executeBatFileWithNewWindow(String file, boolean isCloseWindow) {
   String cmdCommand;
   if (isCloseWindow) {
      cmdCommand = "cmd.exe /c start" + file;
   } else {
      cmdCommand = "cmd.exe /k start" + file;
   }
   StringBuilder stringBuilder = new StringBuilder();
   Process process;
   try {
      process = Runtime.getRuntime().exec(cmdCommand);
      BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(process.getInputStream(), "GBK"));
      String line;
      while ((line = bufferedReader.readLine()) != null) {
         stringBuilder.append(line).append(" ");
      }
      return stringBuilder.toString();
   } catch (Exception e) {
      e.printStackTrace();
      return null;
   }
}

/**
 * 执行bat脚本
 *
 * @param batScript 脚本内容
 * @param location  脚本存储路径
 * @return 结果
 */
public static String executeBatScript(String batScript, String location) {
   StringBuilder stringBuilder = new StringBuilder();
   FileWriter fw = null;
   try {
      //生成bat文件
      fw = new FileWriter(location);
      fw.write(batScript);
      fw.close();
   } catch (IOException e) {
      e.printStackTrace();
   }
   Process process;
   try {
      process = Runtime.getRuntime().exec(location);
      BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(process.getInputStream(), "GBK"));
      String line;
      while ((line = bufferedReader.readLine()) != null) {
         stringBuilder.append(line).append(" ");
      }
      return stringBuilder.toString();
   } catch (Exception e) {
      e.printStackTrace();
      return null;
   }
}

/**
 * 执行脚本,不停止,并输出执行结果
 *
 * @param batScript 脚本内容
 * @param location  bat文件生成地址
 */
public void executeBatScriptAlways(String batScript, String location) {
   FileWriter fw = null;
   try {
      //生成bat文件
      fw = new FileWriter(location);
      fw.write(batScript);
      fw.close();
   } catch (IOException e) {
      e.printStackTrace();
   }
   StringBuilder stringBuilder = new StringBuilder();
   //运行bat文件
   Process process;
   try {
      process = Runtime.getRuntime().exec(location);
      BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(process.getInputStream(), "GBK"));
      String line;
      while ((line = bufferedReader.readLine()) != null) {
         System.out.println(line);
      }
   } catch (Exception e) {
      e.printStackTrace();
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr_GGI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值