Java web

Java 调用windows .exe

2019-01-25  本文已影响0人  咖啡机an
    public static void runExe() throws IOException {
        Process process = null;
        try {
            process = Runtime.getRuntime().exec(exePath);
            //防止程序阻塞,需要把缓存中的内容消费掉
            taskExecutor.execute(new InputStreamRunnable(process.getErrorStream(), filePath, 1));
            taskExecutor.execute(new InputStreamRunnable(process.getInputStream(), null, 1));
            process.waitFor();
            LOGGER.info(url);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (process != null) {
                process.destroy();
            }
        }
    }
public class InputStreamRunnable implements Runnable {
    private static final Logger LOGGER = LoggerFactory.getLogger(InputStreamRunnable.class);
    private BufferedReader bReader = null;
    private String fileUrl = null;
    private Integer logOut = null;
    public InputStreamRunnable(InputStream is, String url, Integer logOut) {
        try {
            bReader = new BufferedReader(new InputStreamReader(new BufferedInputStream(is), "GBK"));
            fileUrl = url;
            this.logOut = logOut;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        StringBuffer line = new StringBuffer();
        try {
            String str = "";
            while ((str = bReader.readLine()) != null) {
                line.append(str).append(System.lineSeparator());
            }
            if (logOut != null && logOut == 1) {
                LOGGER.info(line.toString());
                if (!StringUtils.isEmpty(fileUrl)) {
                    LOGGER.info( line.toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bReader != null) {
                    bReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读