/**
 * <pre>
 * Copyright 2014,2015 阿信sxq(songxinqiang@vip.qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </pre>
 */
/*
 * 创建时间：2015年8月30日--下午10:23:14
 * 作者：阿信sxq 使用Windows平台下的Eclipse(STS)创建<br>
 */
package cn.songxinqiang.tool;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统命令执行工具类，提供对系统命令的执行操作<br>
 * 执行系统命令，获取输出或者不获取输出，支持linux或者windows系统，输出已经处理了乱码的情况
 *
 * <p>
 * 众里寻她千百度, 蓦然回首, 那人却在灯火阑珊处.
 * </p>
 * 
 * @author 阿信sxq-2015年8月30日
 *
 */
public final class SystemCmdExec {

    private static final Runtime run;

    static {
        run = Runtime.getRuntime();
    }

    /**
     * 执行系统命令，返回执行是否成功
     * 
     * @author 阿信sxq-2015年8月30日
     * @param cmd
     *            命令
     * @return 仅当操作成功返回true
     */
    public final boolean runCmd(String cmd) {
        boolean flag = false;

        try {
            run.exec(cmd);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }

    /**
     * 执行系统命令返回结果字符串，结果按照一行一个字符串的形式封装为一个列表<br>
     * 该方法会产生阻塞，等待执行结束后才会返回
     * 
     * @author 阿信sxq-2015年8月30日
     * @param cmd
     *            命令
     * @return 命令所产生的所有输出，等到输出完成后才会返回
     */
    public final List<String> runCmdForString(String cmd) {
        List<String> strList = new ArrayList<String>();

        BufferedInputStream in = null;
        BufferedReader inBr = null;
        try {
            Process p = run.exec(cmd);
            in = new BufferedInputStream(p.getInputStream());
            inBr = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            String lineStr = null;
            while ((lineStr = inBr.readLine()) != null) {
                strList.add(lineStr);
            }
            p = null;
            lineStr = null;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inBr != null) {
                try {
                    inBr.close();
                } catch (IOException e) {}
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {}
            }
            inBr = null;
            in = null;
        }

        return strList;
    }

}

@@ -27,17 +27,17 @@
 2014,201
-5
+6
  %E9%98%BF%E4%BF%A1sxq(s
@@ -63,16 +63,17 @@
 com).%0A *
+ 
 %0A * Lice
@@ -245,16 +245,17 @@
 se at%0A *
+ 
 %0A *     
@@ -300,16 +300,17 @@
 E-2.0%0A *
+ 
 %0A * Unle
@@ -1732,24 +1732,105 @@
 %EF%BC%8C%E7%AD%89%E5%BE%85%E6%89%A7%E8%A1%8C%E7%BB%93%E6%9D%9F%E5%90%8E%E6%89%8D%E4%BC%9A%E8%BF%94%E5%9B%9E
+.%3Cbr%3E%0A     * %E8%AF%BB%E5%8F%96%E5%91%BD%E4%BB%A4%E7%9A%84%E8%BE%93%E5%87%BA%E7%BB%93%E6%9E%9C%E6%97%B6%E4%BD%BF%E7%94%A8%E7%BC%96%E7%A0%81%E6%A0%BC%E5%BC%8F%E4%B8%BA%7B@code UTF-8%7D,%E8%BF%99%E4%B8%AA%E7%BC%96%E7%A0%81%E5%9C%A8Linux%E4%B8%8A%E4%B8%8D%E4%BC%9A%E5%87%BA%E7%8E%B0%E4%B9%B1%E7%A0%81%EF%BC%8C%E4%BD%86%E6%98%AF%E5%9C%A8Windows%E4%B8%8A%E5%B0%B1%E4%BC%9A%E6%9C%89%E4%B9%B1%E7%A0%81%E7%9A%84%E5%8F%AF%E8%83%BD
 %0A     * %0A   
@@ -1843,34 +1843,43 @@
 uthor %E9%98%BF%E4%BF%A1sxq-
+-
 201
-5%E5%B9%B48%E6%9C%8830%E6%97%A5
+6%E5%B9%B41%E6%9C%8819%E6%97%A5%0A     * 
 %0A     * @par

修改执行系统命令的工具类，实现读取命令结果输出不乱码(Linux)