Java-ping测试返回延迟
in 默认分类 with 0 comment

Java-ping测试返回延迟

in 默认分类 with 0 comment

使用java来调用系统的ping命令。


    public static void main(String[] args) {

        BufferedReader br = null;
        try{
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec("ping " + "www.baidu.com");
            InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), "GB2312");
            br = new BufferedReader(inputStreamReader);
            String line = null;
            StringBuffer sb = new StringBuffer();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            if(!sb.toString().contains("平均")){
                System.out.println("无网络"); ;
            }
            else{
                System.out.println(sb.toString().substring(sb.toString().lastIndexOf("平均")+5,sb.length()));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
Responses