In Windows, you can use the ping command in CMD to detect network connection conditions, as follows:
When the network connection is normal:
When the network is not connected:
In Java, you can call the ping command to determine whether the network is connected properly:
Package Module.system.common;import Java.io.bufferedreader;import Java.io.ioexception;import java.io.InputStream; Import java.io.inputstreamreader;/** * Determine network connection status. * @author NAGSH * */public class Netstate {public boolean isconnect () {Boolean connect = false; Runtime runtime = Runtime.getruntime (); Process Process;try {process = Runtime.exec ("ping" + "www.baidu.com"); InputStream is = Process.getinputstream (); InputStreamReader ISR = new InputStreamReader (IS); BufferedReader br = new BufferedReader (ISR); String line = null; StringBuffer sb = new StringBuffer (); while (line = Br.readline ())! = null) {sb.append (line); } System.out.println ("Return value:" +SB); Is.close (); Isr.close (); Br.close (); if (null! = SB &&!sb.tostring (). Equals ("")) {String logstring = ""; if (sb.tostring (). IndexOf ("TTL") > 0) {//network unblocked connect = True } else {//network not unblocked connect = false; }}} catch (IOException e) {e.printstacktrace ();} return connect; }public static void Main (string[] args) {netstate netstate = new Netstate (); System.out.println (Netstate.isconnect ());}}
Using Java to detect network connection status