栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Boot 执行 cmd 命令行

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot 执行 cmd 命令行

@GetMapping(value = "/startProgram")
    @CrossOrigin
    public Boolean startProgram(@RequestParam(value = "ditamap") String ditamap) {
//        String programPath = "D:\develop\oxygen-publishing-engine-3.x\bin\dita.bat -f pdf-css-html5 -i E:\ceshi\map.ditamap -o E:\ceshi\output -v";

        try {
            List commands = new ArrayList<>();
//            commands.add("D:/develop/oxygen-publishing-engine-3.x/bin/dita.bat");
//            commands.add("-f");
//            commands.add("pdf-css-html5");
//            commands.add("-i");
//            commands.add("E:/ceshi/" + ditamap);
//            commands.add("-o");
//            commands.add("E:/ceshi/output");
//            commands.add("-v");
            commands.add("/home/oxygen-publishing-engine-3.x/bin/dita");
            commands.add("-f");
            commands.add("pdf-css-html5");
            commands.add("-i");
            String path = "/home/cms/to/" + ditamap;
            File file = new File(path);
            if (!file.exists()){
                log.error("ditaMap不存在" + path);
                return false;
            }
            commands.add("/home/cms/to/" + ditamap);
            commands.add("-o");
            commands.add("/home/cms/to/out");
            commands.add("-v");
            ProcessBuilder pb = new ProcessBuilder(commands);
            pb.redirectErrorStream(true);
            try {
                Process p = pb.start();
                InputStream inputStream = p.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "gbk");
                int len  = -1;
                char[] c = new char[1024];
                StringBuffer st = new StringBuffer();
                while ((len = inputStreamReader.read(c)) != -1){
                    String s = new String(c, 0, len);
                    st.append(s);
                    log.info(s);
                }
                inputStream.close();
            } catch (Exception e) {
                log.error("exception: " + e);
                return false;
            }
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/778051.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号