13693261870
2024-11-07 8bbb8a2caf8720d74fb2aca31abdc6ef3ab9d13c
src/main/java/com/se/simu/service/UwService.java
@@ -79,7 +79,8 @@
    public String callExe(DataPo data) throws Exception {
        String cmd = String.format("%s %d %s", config.getSolverBat(), WebHelper.getCpuCores(), data.getInPath() + ".json");
        return exec(cmd);
        //return exec(cmd);
        return execCmdLine(cmd);
    }
    private String exec(String cmd) throws Exception {
@@ -96,11 +97,12 @@
            StringBuilder sb = new StringBuilder();
            while ((line = nr.readLine()) != null) {
                sb.append(line);
                log.info(line);
            }
            String errorLine;
            while ((errorLine = er.readLine()) != null) {
                log.warn(errorLine);
                log.error(errorLine);
            }
            // 等待程序执行结束并输出状态
@@ -136,6 +138,71 @@
        }
    }
    /**
     * 执行命令行,并等待命令执行完毕
     *
     * https://www.cnblogs.com/stars-one/p/16482964.html
     * @param cmd 命令,window记得要使用cmd /c开头,如cmd /c ipconfig
     * @throws IOException
     * @throws InterruptedException
     */
    private String execCmdLine(String cmd) throws IOException, InterruptedException {
        Process process = Runtime.getRuntime().exec(cmd);
        new Thread(() -> {
            InputStreamReader ir = null;
            BufferedReader br = null;
            try {
                ir = new InputStreamReader(process.getErrorStream(), "GBK");
                br = new BufferedReader(ir);
                String line;
                while ((line = br.readLine()) != null) {
                    log.error(line);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != br) br.close();
                    if (null != ir) ir.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        StringBuilder sb = new StringBuilder();
        //new Thread(() -> {
            InputStreamReader ir = null;
            BufferedReader br = null;
            try {
                ir = new InputStreamReader(process.getInputStream(), "GBK");
                br = new BufferedReader(ir);
                String line;
                while ((line = br.readLine()) != null) {
                    log.info(line);
                    sb.append(line);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != br) br.close();
                    if (null != ir) ir.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        //}).start();
        process.waitFor();
        process.destroy();
        return sb.toString();
    }
    public void copeWaterFiles() {
        //
    }