java 通过ping的方式返回延迟状态

 // 判断网络状态
    public String isConnect() throws Exception {
        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("平均")){
                return "无网络";
            }
            else{
                return sb.toString().substring(sb.toString().lastIndexOf("平均")+5,sb.length());
            }
        }catch (Exception e){
            throw new Exception();
        }finally {
            if (br != null){
                br.close();
            }
        }
    }

调用

public static void main(String[] args) {
        String net = new State().isConnect();
        System.out.println(net);
    }

你可能感兴趣的:(utility,java,网络)