获取服务所在机器IP地址
2015-10-13 11:29
316 查看
前段时间做定时任务的时候,需要在线上的服务器中的某一台执行定时任务,没有找到更好的方法,就想用IP匹配的方法,执行相应的方法。 获取服务器IP:
public String getLocalIp() { String ip = ""; try { // 遍历服务器的网卡地址 for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() && inetAddress.isSiteLocalAddress()) { ip = inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { logger.error("getLocalIp 获取服务器IP异常:" + ex); } logger.info("getLocalIp 获取服务器IP = :" + ip); return ip; }
相关文章推荐
- Hibernate之hello world
- JAVA中如何对double或者float的浮点数进行精度计算
- Python递归查找文件(os.walk, os.path, fnmatch)
- 算法导论-第7章-快速排序算法
- apache重写字段详细说明
- 网络层路由算法距离向量算法和链路状态算法
- 异常:java.lang.NoClassDefFoundError: com.android.volley.toolbox.Volley
- windos 开启openssl
- 遭遇“HTTP 错误 500.19 无法访问请求的页面,因为该页的相关配置数据无效。”
- xUtils的使用教程
- linux的ssh的免密码登录设置
- C#遍历文件夹后上传文件夹中所有文件错误案例分析
- swift 与 OC之间的关系
- Mysql用户权限管理
- java使用poi创建excel文件
- Practical Python and OpenCV + Case Studies
- 统计一个目录下所有普通文件的总行数
- 测试
- Linux USB子系统(1)
- 一个经验丰富的网站建设程序员的CSS资料