中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

使用java執(zhí)行命令簡易封裝類

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
在java中有時我們會調用系統(tǒng)命令或批處理或shell腳本
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public abstract class ExecCommand {
    private static final Log log = LogFactory.getLog(ExecCommand.class);

    /**
     * 
     * @描述 在單獨的進程中執(zhí)行指定的字符串命令。
     * @作者 鐘誠
     * @日期 Sep 9, 2011
     * @時間 5:15:48 PM
     * @param command 一條指定的系統(tǒng)命令
     * @throws IOException
     */
    public void exec(String command) throws IOException {
        exec(command , null , null);
    }
    /**
     * 
     * @描述 在有指定工作目錄的獨立進程中執(zhí)行指定的字符串命令。
     * @作者 鐘誠
     * @日期 Sep 17, 2011
     * @時間 11:17:59 AM
     * @param command 一條指定的系統(tǒng)命令
     * @param workpath 子進程的工作目錄;如果子進程應該繼承當前進程的工作目錄,則該參數(shù)為null。
     * @throws IOException
     */
    public void exec(String command,String workpath) throws IOException {
        exec(command , null , workpath);
    }

    /**
     * 
     * @描述 在有指定環(huán)境和工作目錄的獨立進程中執(zhí)行指定的字符串命令。
     * @作者 鐘誠
     * @日期 Sep 17, 2011
     * @時間 11:21:28 AM
     * @param command 一條指定的系統(tǒng)命令
     * @param envp 環(huán)境變量,字符串數(shù)組,其中每個元素的環(huán)境變量設置格式為 name=value;如果子進程應該繼承當前進程的環(huán)境,則為null。
     * @param path 子進程的工作目錄;如果子進程應該繼承當前進程的工作目錄,則該參數(shù)為null。
     * @throws IOException
     */
    public void exec(String command,String[] envp,String workpath) throws IOException {
        InputStream is = null;
        BufferedInputStream in = null;
        BufferedReader br = null;
        try {
            File dir=null;
            if(null != workpath)
                dir=new File(workpath);
            log.info("【COMMAND】>>> "+command);
            // InputStream is = Runtime.getRuntime().exec(new String[]{"ping","127.0.0.1"}).getInputStream();
            is = Runtime.getRuntime().exec(command,envp,dir).getInputStream();
            in = new BufferedInputStream(is);
            br = new BufferedReader(new InputStreamReader(in));
            String ss = "";
            while ((ss = br.readLine()) != null) {
                lineHandler(ss);
            }
        } finally {
            if (null != br)
                br.close();
            if (null != in)
                in.close();
            if (null != is)
                is.close();
        }
    }

    /**
     * 
     * @描述 在單獨的進程中執(zhí)行指定的命令和參數(shù)。
     * @作者 鐘誠
     * @日期 Sep 9, 2011
     * @時間 5:15:48 PM
     * @param commands 包含所調用命令及其參數(shù)的數(shù)組。例如:new String[]{"/home/user1/test.sh","arg1","arg2"};
     * @throws IOException
     */
    public void exec(String[] commands) throws IOException {
        exec(commands , null , null);
    }
    /**
     * 
     * @描述 在有指定工作目錄的獨立進程中執(zhí)行指定的字符串命令。
     * @作者 鐘誠
     * @日期 Sep 17, 2011
     * @時間 11:17:59 AM
     * @param commands 包含所調用命令及其參數(shù)的數(shù)組。例如:new String[]{"/home/user1/test.sh","arg1","arg2"};
     * @param workpath 子進程的工作目錄;如果子進程應該繼承當前進程的工作目錄,則該參數(shù)為null。
     * @throws IOException
     */
    public void exec(String[] commands,String workpath) throws IOException {
        exec(commands , null , workpath);
    }

    /**
     * 
     * @描述 在有指定環(huán)境和工作目錄的獨立進程中執(zhí)行指定的字符串命令。
     * @作者 鐘誠
     * @日期 Sep 9, 2011
     * @時間 5:18:00 PM
     * @param commands 包含所調用命令及其參數(shù)的數(shù)組。例如:new String[]{"/home/user1/test.sh","arg1","arg2"};
     * @param envp 環(huán)境變量,字符串數(shù)組,其中每個元素的環(huán)境變量設置格式為 name=value;如果子進程應該繼承當前進程的環(huán)境,則為null。
     * @param path 子進程的工作目錄;如果子進程應該繼承當前進程的工作目錄,則該參數(shù)為null。
     * @throws IOException 
     */
    public void exec(String[] commands,String[] envp , String workpath) throws IOException {
        InputStream is = null;
        BufferedInputStream in = null;
        BufferedReader br = null;
        try {
            File dir=null;
            if(null != workpath)
                dir=new File(workpath);
            log.info("【COMMAND】>>>:"+getCommandString(commands));
            is = Runtime.getRuntime().exec(commands,envp,dir).getInputStream();
            in = new BufferedInputStream(is);
            br = new BufferedReader(new InputStreamReader(in));
            String ss = "";
            while ((ss = br.readLine()) != null) {
                lineHandler(ss);
            }
        } finally {
            if (null != br)
                br.close();
            if (null != in)
                in.close();
            if (null != is)
                is.close();
        }
    }
    /**
     * 
     * @描述 僅為日志輸出,無其他作用
     * @作者 鐘誠
     * @日期 Sep 13, 2011
     * @時間 1:48:06 PM
     * @param commands
     * @return
     */
    private String getCommandString(String[] commands){
        StringBuffer sb=new StringBuffer();
        for(String command:commands){
            sb.append(command);
            sb.append(" ");
        }
        return sb.toString();
    }
    /**
     * 
     * @描述 行處理
     * @作者 鐘誠
     * @日期 Sep 9, 2011
     * @時間 5:22:11 PM
     * @param lineStr
     */
    protected abstract void lineHandler(String lineStr);
}

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ExecuteCommand extends ExecCommand {
    private static final Log log = LogFactory.getLog(ExecuteCommand.class);

    /**
     * @描述 執(zhí)行命令返回行內容處理默認直接輸出到日志<br />
     *       如果需要自定義處理請覆蓋此方法。
     * @作者 鐘誠
     * @時間 2011-09-09 17:15
     */
    @Override
    protected void lineHandler(String lineStr) {
        log.info(lineStr);
    }
}

public class ExecCommandTest {
    public static void main(String[] args) throws Exception {
        // 默認處理
        ExecCommand exe1 = new ExecuteCommand();
        exe1.exec("c:\\ls.bat");
        // 自定義處理
        ExecCommand exe2 = new ExecuteCommand() {
            @Override
            protected void lineHandler(String lineStr) {
                System.out.println(lineStr);
            }
        };
        exe2.exec("c:\\ls.bat","c:\\");
        // 多個參數(shù)
        ExecCommand exe3 = new ExecuteCommand();
        exe3.exec(new String[] { "ping", "127.0.0.1" });
    }
}

標簽: 腳本

版權申明:本站文章部分自網絡,如有侵權,請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:PHP 文件上傳示例

下一篇:PHP實現(xiàn)直接在頁面上執(zhí)行SQL語句