如题,我想获取本机ip,在网上找了很多,有的获取了几个ip,有个还获取的个错误的东西:(0:0:0:0:0:0:0)
//获取终端Ip
public static String GetIp() {
InetAddress ia = null;
try {
ia = InetAddress.getLocalHost();
String localip = ia.getHostAddress();
return localip;
} catch (Exception e) {
return null;
}
}
import java.net.InetAddress;
import java.net.UnknownHostException;
public class AA {
/**
* @param args
*/
public static void main(String[] args) {
InetAddress address = null;
try {
address = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("this is address:" + address);
String hostName = address.getHostName();
String hostAddress = address.getHostAddress();
System.out.println("this is host name:" + hostName);
System.out.println("this is host address:" + hostAddress);
byte[] address4Byte = address.getAddress();
System.out.println("this is address4Byte len:" + address4Byte.length);
for (int i = 0; i < address4Byte.length; i++) {
System.out.print(address4Byte[i]);
}
}
}