获取本地ip ,jdk1.6好使的方法
public static void main(String[] args) throws SocketException {
List<String> addrs = Lists.newArrayList( );
Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces( );
while( ifaces.hasMoreElements( ) ) {
NetworkInterface iface = ifaces.nextElement( );
for( InterfaceAddress iaddr : iface.getInterfaceAddresses( ) ) {
InetAddress addr = iaddr.getAddress( );
if( addr instanceof Inet4Address ) {
if( !addr.isMulticastAddress( ) && !addr.isLoopbackAddress( ) && !addr.isLinkLocalAddress( ) && !addr.isSiteLocalAddress( ) && !"192.168.122.1".equals( addr.getHostAddress( ) ) ) {
addrs.add( addr.getHostAddress( ) );
}
}
if( addr instanceof Inet4Address ) {
if( !addr.isMulticastAddress( ) && !addr.isLoopbackAddress( ) && !addr.isLinkLocalAddress( ) && !addrs.contains( addr.getHostAddress( ) ) && !"192.168.122.1".equals( addr.getHostAddress( ) ) ) {
addrs.add( addr.getHostAddress( ) );
}
}
}
}
System.out.println(addrs);
}