package com.se.system.utils; import java.net.*; @SuppressWarnings("ALL") public class ConnectUtils { public static boolean pingIp(String ip) { try { InetAddress inetAddress = InetAddress.getByName(ip); return inetAddress.isReachable(1500); } catch (Exception ex) { return false; } } public static boolean isReachable(String addr) { try { String[] strs = addr.split(":"); InetSocketAddress socketAddress = new InetSocketAddress(strs[0], strs.length < 2 || StringUtils.isEmpty(strs[1]) ? 80 : Integer.parseInt(strs[1])); Socket socket = new Socket(); socket.connect(socketAddress, 1500); socket.close(); return true; } catch (Exception ex) { return false; } } public static boolean testUrl(String uri, String method) { try { URL url = new URL(uri); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod(StringUtils.isEmpty(method) ? "GET" : method); con.setConnectTimeout(1000); con.setReadTimeout(1000); int code = con.getResponseCode(); return code >= 200 && code <= 399; } catch (Exception ex) { return false; } } }