InetAddress byName = InetAddress.getByName("173.39.161.140");
System.out.println(byName);
System.out.println(byName.isReachable(1000));
Run Code Online (Sandbox Code Playgroud)
为什么要isReachable回来false?我可以ping IP.
你如何检查你是否可以通过java连接到互联网?一种方法是:
final URL url = new URL("http://www.google.com");
final URLConnection conn = url.openConnection();
... if we got here, we should have net ...
Run Code Online (Sandbox Code Playgroud)
但是,是否有更适合执行该任务的东西,特别是如果您需要经常进行连续检查并且极有可能丢失互联网连接?