Test.java 2.54 KB
package com.thinkgem.jeesite.tools;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;

public class Test { 
	public static void main(String[] args)   {
		 List<String> list = getIPList();
	    
	        
	        if(list.contains("172.16.58.79")) {
	        	 System.out.println("true"  );
	        }
	        
	}
	
	
	 public static List<String> getIPList() {
	        
		 
		 	List<String> res_list = Lists.newArrayList();
	        Enumeration<NetworkInterface> e1;
	        try {
	            e1 = NetworkInterface.getNetworkInterfaces();
	            while (e1.hasMoreElements()) {
	                NetworkInterface ni = e1.nextElement();
	                
	                List<String> ips =  Lists.newLinkedList();
	              
	                
	                Enumeration<InetAddress> e2 = ni.getInetAddresses();
	                while (e2.hasMoreElements()) {
	                    InetAddress ia = e2.nextElement();
	                    if(ia instanceof Inet6Address) {
	                        continue; // omit IPv6 address
	                    }
	                    ips.add(ia.getHostAddress());                    
	                }
	                res_list.addAll(ips);
	            }
	        } catch (SocketException e) {
	            e.printStackTrace();
	        }
	        return res_list;
	    }
	
	 public static Map<String, List<String>> getLocalIP() {
	        Map<String, List<String>> map = new HashMap<String, List<String>>();
	        Enumeration<NetworkInterface> e1;
	        try {
	            e1 = NetworkInterface.getNetworkInterfaces();
	            while (e1.hasMoreElements()) {
	                NetworkInterface ni = e1.nextElement();
	                
	                List<String> ips =  Lists.newLinkedList();
	                map.put(ni.getName(), ips);
	                
	                Enumeration<InetAddress> e2 = ni.getInetAddresses();
	                while (e2.hasMoreElements()) {
	                    InetAddress ia = e2.nextElement();
	                    if(ia instanceof Inet6Address) {
	                        continue; // omit IPv6 address
	                    }
	                    ips.add(ia.getHostAddress());                    
	                }
	            }
	        } catch (SocketException e) {
	            e.printStackTrace();
	        }
	        return map;
	    }
	
}