Java Runtime.exec 執行shell命令

jopen 9年前發布 | 13K 次閱讀 shell命令 Java開發

 最近做一個團購文件下載服務的時候,自己偷懶不想寫文件下載的邏輯,而且Linux的curl 命令非常強大,于是乎想直接通過Java來執行curl命令做下載。

       在Java中,你可以通過Runtime.getRuntime().exec 來執行外部的shell命令。

    Process proc = Runtime.getRuntime().exec("ping www.baidu.com");

            InputStream in = proc.getInputStream();  

            BufferedReader br = new BufferedReader(new InputStreamReader(in, "GBK"));  
            String line = null;  

            while((line=br.readLine())!=null){  
                System.out.println(line);  
            }  </pre><br />

下面通過執行一個簡單的 ping命令并打印其輸出 作為示例。

    package com.ricky.java.sms;

import java.io.BufferedReader;  
import java.io.ByteArrayOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.InputStreamReader;  

public class RuntimeTest {  

    public static void main(String[] args) {  

//      methodA();  

        methodB();  
    }  

    public static void methodB() {  

        BufferedReader br = null;  
        try {  

            Process proc = Runtime.getRuntime().exec("ping www.baidu.com");  

            InputStream in = proc.getInputStream();  

            br = new BufferedReader(new InputStreamReader(in, "GBK"));  
            String line = null;  

            while((line=br.readLine())!=null){  
                System.out.println(line);  
            }  

        } catch (IOException e) {  
            e.printStackTrace();  
        }finally{  
            if(br!=null){  
                try {  
                    br.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
    }  

    public static void methodA() {  

        InputStream in = null;  
        try {  

            Process proc = Runtime.getRuntime().exec("ping www.baidu.com");  

            in = proc.getInputStream();  

            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            in = proc.getInputStream();  

            byte[] buf = new byte[1024];  
            int len = 0;  
            while ((len = in.read(buf, 0, 1024)) != -1) {  
                baos.write(buf, 0, len);  
            }  

            byte[] arr = baos.toByteArray();  
            String result = new String(arr, "GBK");  

            System.out.println("result="+result);  

        } catch (IOException e) {  
            e.printStackTrace();  
        }finally{  
            if(in!=null){  
                try {  
                    in.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
    }  

}  </pre><br />

打印的結果如下:



正在 Ping www.a.shifen.com [180.149.131.205] 具有 32 字節的數據:
來自 180.149.131.205 的回復: 字節=32 時間=1ms TTL=56
來自 180.149.131.205 的回復: 字節=32 時間=1ms TTL=56
來自 180.149.131.205 的回復: 字節=32 時間=1ms TTL=56
來自 180.149.131.205 的回復: 字節=32 時間=1ms TTL=56


180.149.131.205 的 Ping 統計信息:
    數據包: 已發送 = 4,已接收 = 4,丟失 = 0 (0% 丟失),
往返行程的估計時間(以毫秒為單位):
    最短 = 1ms,最長 = 1ms,平均 = 1ms

來自:http://blog.csdn.net/top_code/article/details/43052399

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!