思路:利用runtime调用 ping命令。
package test.network;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class TestNetworkConnection {
public static void main(String[] args) {
new TestNetworkConnection().start();
}
public void start(){
Runtime runtime=Runtime.getRuntime();
try {
Process process=runtime.exec("ping "+"10.4.46.194");
InputStream is=process.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
String line=null;
StringBuffer sb=new StringBuffer();
while((line=br.readLine())!=null){
sb.append(line);
// System.out.println("返回值为:"+line);
}
is.close();
isr.close();
br.close();
if(null != sb && !sb.toString().equals("")){
if(sb.toString().indexOf("TTL")>0){
System.out.println("网络通畅");
}else{
System.out.println("网络有问题");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
refurl:http://www.itpub.net/thread-340898-1-1.html