美文网首页
java 调用多行cmd命令

java 调用多行cmd命令

作者: YU_XI | 来源:发表于2017-09-21 17:10 被阅读0次

    publicString add(

    HttpServletRequest request, HttpServletResponse response) {

    JSONObject jo =newJSONObject();

    BufferedReader bReader=null;

    InputStreamReader sReader=null;

    try

    {

    String[] cmd = {"cmd","/k","cd /d C: & cd / & cd %HOMEPATH% & echo http://YU_XI:123456@192.168.0.149%3a3000>> .git-credentials"+

    "& cd / & cd C:/Temp/FileLibrary/xieyx/2017-09-15_14-09-551/syslink222  &\"C:/Program Files (x86)/Git/bin/git.exe\"init "+

    "&\"C:/Program Files (x86)/Git/bin/git.exe\"remote add origin http://192.168.0.149:3000/YU_XI/ceshi3.git "+

    "&\"C:/Program Files (x86)/Git/bin/git.exe\"add . &\"C:/Program Files (x86)/Git/bin/git.exe\"commit -m\"model\""+

    " &\"C:/Program Files (x86)/Git/bin/git.exe\"config --global credential.helper store &\"C:/Program Files (x86)/Git/bin/git.exe\"push origin master  & calc"};

    Process p = Runtime.getRuntime().exec(cmd);

    /*为"错误输出流"单独开一个线程读取之,否则会造成标准输出流的阻塞*/

    Thread t=newThread(newInputStreamRunnable(p.getErrorStream(),"ErrorStream"));

    t.start();

    /*"标准输出流"就在当前方法中读取*/

    BufferedInputStream bis =newBufferedInputStream(p.getInputStream());

    if("GBK"!=null&&"GBK".length()!=0)

    {

    sReader =newInputStreamReader(bis,"GBK");//设置编码方式

    }

    else

    {

    sReader =newInputStreamReader(bis,"GBK");

    }

    bReader=newBufferedReader(sReader);

    StringBuilder sb=newStringBuilder();

    String line;

    while((line=bReader.readLine())!=null)

    {

    sb.append(line);

    sb.append("/n");

    }

    bReader.close();

    p.destroy();

    returnsb.toString();

    }

    catch(Exception e)

    {

    e.printStackTrace();

    return"";

    }

    finally

    {

    }

    }

    public staticString excuteCmd_multiThread(String[] cmd, String encoding)

    {

    BufferedReader bReader=null;

    InputStreamReader sReader=null;

    try

    {

    Process p = Runtime.getRuntime().exec(cmd);

    /*为"错误输出流"单独开一个线程读取之,否则会造成标准输出流的阻塞*/

    Thread t=newThread(newInputStreamRunnable(p.getErrorStream(),"ErrorStream"));

    t.start();

    /*"标准输出流"就在当前方法中读取*/

    BufferedInputStream bis =newBufferedInputStream(p.getInputStream());

    if(encoding!=null&& encoding.length()!=0)

    {

    sReader =newInputStreamReader(bis,encoding);//设置编码方式

    }

    else

    {

    sReader =newInputStreamReader(bis,"GBK");

    }

    bReader=newBufferedReader(sReader);

    StringBuilder sb=newStringBuilder();

    String line;

    while((line=bReader.readLine())!=null)

    {

    sb.append(line);

    sb.append("/n");

    }

    bReader.close();

    p.destroy();

    returnsb.toString();

    }

    catch(Exception e)

    {

    e.printStackTrace();

    //        return ErrorString;

    return"";

    }

    finally

    {

    }

    }

    相关文章

      网友评论

          本文标题: java 调用多行cmd命令

          本文链接:https://www.haomeiwen.com/subject/koktextx.html