公共类ConnetionType {
public static void main(String[] args) {NetworkInterface ni=new NetworkInterface();
}
}
public class networktest {
public static void main(String[] args) {
try{
Enumeration<NetworkInterface>eni=NetworkInterface.getNetworkInterfaces();
while(eni.hasMoreElements()){
NetworkInterface nii=eni.nextElement();
if(nii.isUp())
System.out.println("you are connected to:"+nii.getDisplayName());
}
}catch(Exception e){
e.printStackTrace();
}
}
}
如果您在Windows计算机上运行,则可以使用命令行:
try {
Process p = Runtime.getRuntime().exec("netsh interface show interface");
String line;
java.io.BufferedReader input = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
int counter = 0;
while ((line = input.readLine()) != null) {
counter++;
if (counter > 3) { // The first lines are headlines, so it can be ignored
System.out.println(line);
}
}
input.close();
} catch (Exception e) {
e.printStackTrace();
}
有关该命令的说明,请参阅here。