`
frenchleaf
  • 浏览: 39024 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

获取本地ip

 
阅读更多
获取本地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);
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics