私有静态获取工具(
字符串tempip=' 127.0.0.1 '; try { if (isip v4 (inet地址. get localhost ().getHostAddress ) ) )
tempip=inet address.get localhost ().getHostAddress );
}catch(unknownhostexceptionE1) {//todo auto-generated catch块
E1 .打印堆栈跟踪(;
}try{
enumeration networks=network interface.getnetworkinterfaces (;
inet地址IP=null;
枚举地址; while(NetWorks.hasmoreelements ) ) )
{
addrs=networks.nextElement ().getInetAddresses ); while(addrs.hasmoreelements ) )
{
ip=addrs.nextElement (; if(IP!=空值
ipinstanceofinet 4地址IP.issitelocaladdress! ip.getHostAddress ().equals () tempip ) )
{if(isipV4(IP.gethostaddress () ) ) returnip.getHostAddress );
}
}
}returntempIP;
}catch(exceptione ) {
System.out.println; thrownewruntimeexception(e;
}
} publicstaticbooleanisipv4(字符串IP地址) {
stringip=' ^ (1 d {2}|2 [0-4]d|25 [0-5]|[1-9]d|[1-9] () ) () () ) )
' (00? d|1d {2}|2 [0-4]d|25 [0-5]|[1-9]d|\ d ]\\."
' (00? d|1d {2}|2 [0-4]d|25 [0-5]|[1-9]d|\ d ]\\."
' (00? d|1d {2}|2 [0-4]d|25 [0-5]|[1-9]d|\d ] $ ';
pattern pattern=pattern.com pile (IP;
matcher matcher=pattern.matcher (IP地址); returnmatcher.matches (;
}