获取IP

java中获取本机IP的一种方法
public String getLocalIP() throws Exception {  
		String ip = "";
	   Enumeration e1 = (Enumeration) NetworkInterface.getNetworkInterfaces();  
	   while (e1.hasMoreElements()) {  
	    NetworkInterface ni = (NetworkInterface) e1.nextElement();  
	    Enumeration e2 = ni.getInetAddresses();  
	    while (e2.hasMoreElements()) {  
	     InetAddress ia = (InetAddress) e2.nextElement();  
	     if (ia instanceof Inet6Address)  
	      continue; // omit IPv6 address  
	     if(ia.getHostAddress()!=""&&ia.getHostAddress()!="127.0.0.1")
	    	 ip=ia.getHostAddress(); 
	    }  
	   }  
	   

你可能感兴趣的:(获取IP)