首页>代码>SpringBoot爬取html生成CHM帮助文档>/springboot-chm/src/main/java/com/simon/springbootchm/util/CommandUtils.java
package com.simon.springbootchm.util;

import com.jfinal.kit.StrKit;
import com.jfinal.template.Engine;
import com.jfinal.template.Template;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * 执行命令工具类
 *
 * @author Simon
 */
public class CommandUtils {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String ROOT_PATH = CommandUtils.class.getClassLoader().getResource("").getPath().substring(1);
    private static final Engine engine = Engine.use().setBaseTemplatePath(ROOT_PATH);

    public static String getPath(String projectName) {
        return ROOT_PATH + projectName;
    }

    public static Template getTemplate(String projectName, String name) {
        return engine.getTemplate(projectName + '/' + name);
    }

    private ProcessBuilder pb;
    private String command;
    private String[] envp;
    private File dir;
    private String[] cmdarray;

    public CommandUtils(String command, String[] envp, String dirPath) {
        this.envp = envp;
        if (dirPath != null) {
            dir = new File(dirPath);
        }
        if (command.length() == 0) {
            throw new IllegalArgumentException("Empty command");
        }
        this.command = command;
        StringTokenizer st = new StringTokenizer(command);
        cmdarray = new String[st.countTokens()];
        for (int i = 0; st.hasMoreTokens(); i++) {
            cmdarray[i] = st.nextToken();
        }
        getPb();
    }

    public CommandUtils(String[] cmdarray, String[] envp, String dirPath) {
        this.envp = envp;
        if (dirPath != null) {
            dir = new File(dirPath);
        }
        this.cmdarray = cmdarray;
        this.command = StrKit.join(cmdarray);
        getPb();
    }

    public ProcessBuilder getPb() {
        if (pb == null) {
            pb = new ProcessBuilder(cmdarray);
            Map<String, String> env = pb.environment();
            if (envp != null) {
                for (String envstring : envp) {
                    if (envstring.indexOf((int) '\u0000') != -1) {
                        envstring = envstring.replaceFirst("\u0000.*", "");
                    }
                    int eqlsign = envstring.indexOf('=', 1);
                    if (eqlsign != -1) {
                        env.put(envstring.substring(0, eqlsign), envstring.substring(eqlsign + 1));
                    }
                }
            }
            pb.redirectErrorStream(true);//重定向错误流到输入流,一起打印,防止缓存满
            pb.directory(dir);
        }
        return pb;
    }

    public void run() {
        logger.info("command:{} dir:{}", command, dir);
        BufferedReader reader = null;
        try {
            Process p = getPb().start();
            reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String s = null;
            int a = 0;
            while ((s = reader.readLine()) != null) {
                if (a > 0) {
                    String join = String.join("", Collections.nCopies(a + 80, "\b"));
                    logger.info(join);
                    a = 0;
                }
                if (!"".equals(s)) {
                    if (s.matches("[^\\n]*\\[[=*>\\s]+\\][^\\n]*")) {
                        a = s.length();
                        logger.info(s);
                    } else {
                        logger.info(s);
                    }
                }
            }
            p.waitFor();
            logger.info("execute end");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void run(String command, String[] envp, String dir) {
        new CommandUtils(command, envp, dir).run();
    }
}
最近下载更多
139465  LV12 2023年3月29日
crosa_Don  LV18 2023年3月2日
最代码官方  LV168 2023年2月19日
最近浏览更多
3334004690  LV10 3月8日
茶茶茶百道qq 2023年9月20日
暂无贡献等级
多加两块钱  LV4 2023年6月12日
skook7  LV2 2023年6月1日
szf123  LV12 2023年5月30日
duanzhouyang  LV10 2023年5月12日
Pro_Guoli 2023年5月12日
暂无贡献等级
fewfsdaf  LV4 2023年4月18日
做你的景天  LV7 2023年4月12日
master_guo  LV7 2023年4月12日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友