public String getLocalHostName() {
String hostName;
try {
InetAddress addr = InetAddress.getLocalHost();
hostName = addr.getHostName();
} catch (Exception ex) {
hostName = "";
}
return hostName;
}
获取IP(多个网卡时获取了多个IP):
[java] view plain copy
public List getNetworkAddress() {
List result = new ArrayList();
Enumeration netInterfaces;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip;
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration addresses=ni.getInetAddresses();
while(addresses.hasMoreElements()){
ip = addresses.nextElement();
if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(':') == -1) {
result.add(ip.getHostAddress());
}
}
}
return result;
} catch (Exception e) {
return null;
}
}