您的位置:首页 > 博客中心 > 编程语言 >

java判断网络是否连通

时间:2022-03-24 18:43

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+"监测网络异常";
		}
	}

热门排行

今日推荐

热门手游