场景

公司服务器非窗口不能发版。 也就是不给登录服务器的高权限用户。
那么怎么解决这个问题呢?

解决方案

为了方便处理问题,可以在服务器上启动个项目来执行脚本。

原理: java的Process类可以运行命令。

拿个网上用烂了的代码作为例子吧:

public class RuntimeTest {
    private static Logger logger = LoggerFactory.getLogger(RuntimeTest.class);
    public static void main(String[] args) {
        RuntimeTest s = new RuntimeTest();
        s.test();
    }
    public void test(){
        Runtime run =Runtime.getRuntime();
        try {
            Process p = run.exec("ping 127.0.0.1");
            InputStream ins= p.getInputStream();
            InputStream ers= p.getErrorStream();
            new Thread(new inputStreamThread(ins)).start();
            p.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    class inputStreamThread implements Runnable{
        private InputStream ins = null;
        private BufferedReader bfr = null;
        public inputStreamThread(InputStream ins){
            this.ins = ins;
            this.bfr = new BufferedReader(new InputStreamReader(ins));
        }
        @Override
        public void run() {
            String line = null;
            byte[] b = new byte[100];
            int num = 0;
            try {
                while((num=ins.read(b))!=-1){
                    logger.info(new String(b,"gb2312"));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

如果将上面的命令String作为参数传给controller,就可以作为接口用了。

示例controller的代码:

@RequestMapping("/index")
    public String index(@RequestBody CommandEntity commandEntity){
        logger.info("l请求开始,入参:{}", JSON.toJSONString(commandEntity));
        Runtime run =Runtime.getRuntime();
        try {
            Process p = run.exec(commandEntity.getCommandString());
            InputStream ins= p.getInputStream();
            InputStream ers= p.getErrorStream();
            new Thread(new inputStreamThread(ins)).start();
//            p.waitFor(); // 是否等待程序执行完毕
            logger.info("请求线程已启动");
            return "请求线程已启动";
        } catch (Throwable e) {
            e.printStackTrace();
            logger.error("请求异常,error:",e);
            return e.getMessage();
        }
    }

    class inputStreamThread implements Runnable{
        private InputStream ins = null;
        private BufferedReader bfr = null;
        public inputStreamThread(InputStream ins){
            this.ins = ins;
            this.bfr = new BufferedReader(new InputStreamReader(ins));
        }
        @Override
        public void run() {
            String line = null;
            byte[] b = new byte[100];
            int num = 0;
            try {
                while((num=ins.read(b))!=-1){
                    logger.info(new String(b,"gb2312"));
                }
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("执行命令出错,error:",e);
            }
        }
    }

其他

多条命令的问题

多条命令可能会和我们的期待不一致:

输入命令:
mkdir a.txt;mdkir b.txt;

期待结果:
创建 a.txt b.txt

实际结果:
创建了 a.txt;mdkir 和 b.txt 

发现了没有,分号并没有起到换行的作用。 而是作为一条命令来执行了。

解决办法。可以将多条命令先用字符串拆为多条命令。再分别执行。

wget拿到的文件比实际的文件小

如果root用户直接wget的话。 文件是正常的(160多M)。
但是通过命令wget的内容,比实际文件小了很多,只有43M。 暂时还不知道为什么。

变通方案:
可以通过项目将文件上传上去。
实际已实现,例如在该项目增加个上传功能。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐