首页 > 代码库 > java判断网络是否连通

java判断网络是否连通

public synchronized static String getIntelIp(String describe,String ip){
		Process p=null;
		BufferedReader in=null;
		try {
			p = Runtime.getRuntime().exec("cmd /c ping -n 1 " + ip); // 此处1变大可以增加精确度,但影响测试速度
			//缓冲读取
			in = new BufferedReader(new InputStreamReader(p
					.getInputStream(),"gbk"));
			String temp = null;
			StringBuffer strBuffer = new StringBuffer();
			while ((temp = (in.readLine())) != null)
				strBuffer.append(temp);
//			 System.out.println(strBuffer.toString().matches(".*\\(\\d?\\d% loss\\).*"));
//			System.out.println(strBuffer);
			in.close();
			if (strBuffer.toString().contains("请求超时")) {
				return describe+"网络连接异常";
			} else {
				return describe+"网络连接正常";
			}
		} catch (IOException e) {
			e.printStackTrace();
			return describe+"监测网络异常";
		}
	}

java判断网络是否连通