首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

JAVA种抓取IP地址

2012-11-23 
JAVA类抓取IP地址import java.net.Inet4Addressimport java.net.InetAddressimport java.net.NetworkInt

JAVA类抓取IP地址

import java.net.Inet4Address;import java.net.InetAddress;import java.net.NetworkInterface;import java.util.Enumeration;public class IPAddrFetcher { public static String getIPInfo() {  String trueIp = null;  try {   Enumeration interfaces = NetworkInterface     .getNetworkInterfaces();   while (interfaces.hasMoreElements()) {    NetworkInterface ni = interfaces.nextElement();    Enumeration inetAddresses = ni.getInetAddresses();    while (inetAddresses.hasMoreElements()) {     InetAddress address = inetAddresses.nextElement();     if (address instanceof Inet4Address) {      if (!address.getHostAddress().equals("127.0.0.1"))       trueIp = address.getHostAddress();     }    }   }  } catch (Exception ex) {   ex.printStackTrace();  }  return trueIp; }}

热点排行