获取IP工具类IPGetUtil
2016-04-28 23:46
141 查看
import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; import org.apache.log4j.Logger; public class IPGetUtil { private static Logger log = Logger.getLogger("IPGetUtil"); /** * * getLocalIpAddress(获取ip) * * @Title: getLocalIpAddress * @param @return * @return String * @throws */ public static String getLocalIpAddress() { StringBuffer ips = new StringBuffer(); Enumeration<NetworkInterface> allNetInterfaces; // 定义网络接口枚举类 try { allNetInterfaces = NetworkInterface.getNetworkInterfaces(); // 获得网络接口 InetAddress ip = null; // 声明一个InetAddress类型ip地址 while (allNetInterfaces.hasMoreElements()) // 遍历所有的网络接口 { NetworkInterface netInterface = allNetInterfaces.nextElement(); Enumeration<InetAddress> addresses = netInterface .getInetAddresses(); // 同样再定义网络地址枚举类 while (addresses.hasMoreElements()) { ip = addresses.nextElement(); if (ip != null && (ip instanceof Inet4Address) && !"127.0.0.1".equals(ip.getHostAddress() .toString())) // InetAddress类包括Inet4Address和Inet6Address { ips.append(ip.getHostAddress()).append("|"); } } } } catch (Exception e) { log.error("获取本地ip异常:" + e); } return ips.toString().substring(0, ips.toString().length() - 1); } }
相关文章推荐
- Xcode 7.2 真机测试 iOS9.3 系统
- 递归扫描目录下的文件(包含子目录)
- xml
- 炒鸡简单:用for循环计算1—100的和。
- 将Http请求输入流转化为字符串工具类StreamHelper
- Activity启动模式launchMode
- Ubuntu 安装
- 初识中间件之消息队列
- shiro 授权方式及其应用
- 对象拷贝工具类BeanutilsCopy
- SDK示例和手柄用例
- data table 手动添加列及行
- LeetCode: 343. Integer Break
- 剖析你的“环境”,是如何保存数据的,大端Or小端
- [杂谈]这个四月
- ZOJ 7th Anniversary Contest
- 328.LeetCode_Linked List_Odd Even Linked List
- SpannableString属性详解
- Json工具类 ---JsonUtil
- Servlet的使用方法详细说明