java百分百获取到机器IP地址及MAC码

已测系统:

windows linux unix

排除127.0.0.1 和 0.0.0.0.1等非正常IP

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;public class IpUtil {private IpUtil(){}/*** 此方法描述的是:获得服务器的IP地址* @author:  zhangyang33@sinopharm.com* @version: 2014年9月5日 下午4:57:15*/public static String getLocalIP() {String sIP = "";InetAddress ip = null;try {boolean bFindIP = false;Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface.getNetworkInterfaces();while (netInterfaces.hasMoreElements()) {if (bFindIP) {break;}NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();Enumeration<InetAddress> ips = ni.getInetAddresses();while (ips.hasMoreElements()) {ip = (InetAddress) ips.nextElement();if (!ip.isLoopbackAddress() && ip.getHostAddress().matches("(\\d{1,3}\\.){3}\\d{1,3}")) {bFindIP = true;break;}}}} catch (Exception e) {OutUtil.error(IpUtil.class, e.getMessage());}if (null != ip) {sIP = ip.getHostAddress();}return sIP;}/*** 此方法描述的是:获得服务器的IP地址(多网卡)* @author:  zhangyang33@sinopharm.com* @version: 2014年9月5日 下午4:57:15*/public static List<String> getLocalIPS() {InetAddress ip = null;List<String> ipList = new ArrayList<String>();try {Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface.getNetworkInterfaces();while (netInterfaces.hasMoreElements()) {NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();Enumeration<InetAddress> ips = ni.getInetAddresses();while (ips.hasMoreElements()) {ip = (InetAddress) ips.nextElement();if (!ip.isLoopbackAddress() && ip.getHostAddress().matches("(\\d{1,3}\\.){3}\\d{1,3}")) {ipList.add(ip.getHostAddress());}}}} catch (Exception e) {OutUtil.error(IpUtil.class, e.getMessage());}return ipList;}/*** 此方法描述的是:获得服务器的MAC地址* @author:  zhangyang33@sinopharm.com* @version: 2014年9月5日 下午1:27:25*/public static String getMacId() {String macId = "";InetAddress ip = null;NetworkInterface ni = null;try {boolean bFindIP = false;Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface.getNetworkInterfaces();while (netInterfaces.hasMoreElements()) {if (bFindIP) {break;}ni = (NetworkInterface) netInterfaces.nextElement();// ----------特定情况,可以考虑用ni.getName判断// 遍历所有ipEnumeration<InetAddress> ips = ni.getInetAddresses();while (ips.hasMoreElements()) {ip = (InetAddress) ips.nextElement();if (!ip.isLoopbackAddress() // 非127.0.0.1&& ip.getHostAddress().matches("(\\d{1,3}\\.){3}\\d{1,3}")) {bFindIP = true;break;}}}} catch (Exception e) {OutUtil.error(IpUtil.class, e.getMessage());}if (null != ip) {try {macId = getMacFromBytes(ni.getHardwareAddress());} catch (SocketException e) {OutUtil.error(IpUtil.class, e.getMessage());}}return macId;}/*** 此方法描述的是:获得服务器的MAC地址(多网卡)* @author:  zhangyang33@sinopharm.com* @version: 2014年9月5日 下午1:27:25*/public static List<String> getMacIds() {InetAddress ip = null;NetworkInterface ni = null;List<String> macList = new ArrayList<String>();try {Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface.getNetworkInterfaces();while (netInterfaces.hasMoreElements()) {ni = (NetworkInterface) netInterfaces.nextElement();// ----------特定情况,可以考虑用ni.getName判断// 遍历所有ipEnumeration<InetAddress> ips = ni.getInetAddresses();while (ips.hasMoreElements()) {ip = (InetAddress) ips.nextElement();if (!ip.isLoopbackAddress() // 非127.0.0.1&& ip.getHostAddress().matches("(\\d{1,3}\\.){3}\\d{1,3}")) {macList.add(getMacFromBytes(ni.getHardwareAddress()));}}}} catch (Exception e) {OutUtil.error(IpUtil.class, e.getMessage());}return macList;}private static String getMacFromBytes(byte[] bytes) {StringBuffer mac = new StringBuffer();byte currentByte;boolean first = false;for (byte b : bytes) {if (first) {mac.append("-");}currentByte = (byte) ((b & 240) >> 4);mac.append(Integer.toHexString(currentByte));currentByte = (byte) (b & 15);mac.append(Integer.toHexString(currentByte));first = true;}return mac.toString().toUpperCase();}}

来自:http://jdkleo.iteye.com/blog/2113782博客

java百分百获取到机器IP地址及MAC码相关推荐

  1. Java 程序获取本机 ip 地址

    Java程序获取本机ip地址: host_ip_list = new ArrayList<String>(); try {for (NetworkInterface networkInte ...

  2. python获取本机 ip地址、mac地址(物理地址)、计算机名、硬盘序列号、CPU序列号、主板序列号、bios序列号、bios uuid号

    文章目录 python获取本机IP地址.mac地址(物理地址).计算机名 物理地址 计算机名.ip地址 硬盘序列号 CPU序列号 主板序列号 bios序列号 bios uuid号 python获取本机 ...

  3. java 获取客户端真实 ip 地址和 mac 地址

    现在都流行显示 ip 归属地,那么我先来获取客户端的真实 ip 地址以及 mac 地址,上代码 import javax.servlet.http.HttpServletRequest; import ...

  4. 微课--Python获取局域网内所有机器IP地址与MAC地址

    开学第一课:一定不要这样问老师Python问题 董付国老师Python系列教材推荐与选用参考 3000道Python习题免费在线练习 ============== 版权声明:由于公众号后台规则问题,本 ...

  5. JAVA如何获取访问真实IP地址?

    解决方法: 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址,如果没有代理,则获取真实ip public static String getIp(HttpServletReques ...

  6. html根据ip和mac地址,关于怎么通过IE浏览器获取客户端的IP地址和Mac地址

    在项目开发中,我们经常会使用到客户端的IP地址.Mac地址和电脑名,下面争对IE浏览器获取IP,Mac和电能名的方法: 需要注意的是,要设置IE浏览器能够加载ActiveX控件,否则会组织获取到相关的 ...

  7. 关于如何通过IE浏览器获取客户端的IP地址和Mac地址

    在项目开发中,我们经常会使用到客户端的IP地址.Mac地址和电脑名,下面争对IE浏览器获取IP,Mac和电能名的方法: 需要注意的是,要设置IE浏览器能够加载ActiveX控件,否则会组织获取到相关的 ...

  8. android程序获取WIFI的IP地址和MAC地址

    转自:http://my.oschina.net/chenj/blog/68680 近日在鼓捣这玩意,拿出来大家分享一下,代码比较简单,如果有不懂的再问 <Button android:id=& ...

  9. Android获取手机WiFi IP地址,MAC地址和网关地址程序实例

    现在博主在上大三,正在学习一些计算机网络的一些概念,知道了局域网ip和网关ip不同的概念,当时还纳了闷了2的32次方好像不咋够用啊,原来是这个样子. 由于之前学过一段时间的Android,同时又在上A ...

最新文章

  1. 3没有样式重置_系统重置新增选项,99%的人不知道怎么选
  2. 用Docker创建Nexus
  3. C++ 二进制转十进制
  4. 双指针算法(四):力扣 88.合并两个有序数组 | 经典例题
  5. 如何打开pr_debug调试信息
  6. android module 加载.so,关于Android Studio主Module与依赖Module同时引入so库的问题
  7. springcloud是如何进行服务治理的
  8. 此C语言功能---A
  9. fpga c语言教程,FPGA实践教程(一)用HLS将c程序生成IPcore
  10. java 多次正则匹配_Java正则多次匹配和多次组匹配
  11. negative和passive的反义词_消极的近义词和反义词
  12. CDP、私域运营及数字营销相关名词一览
  13. 8有价值的电子商务网站设计要点
  14. 二维EMD_BEMDcode
  15. 【C++】操作符重载
  16. 网络规划设计师5天修炼-施游-专题视频课程
  17. 微信小程序开发之页面布局
  18. linux(centos7)测试带宽
  19. 机器学习常用算法原理及优缺点
  20. Spring总结——SpEL表达式

热门文章

  1. Java八大基本类型详解
  2. oracle amm和asmm,AMM和ASMM理解 | 学步园
  3. 掘金网站作者的文章列表文章详情获取-Java网络爬虫系统性学习与实战(14)
  4. 【朴素贝叶斯学习笔记】
  5. 中国地图html+css版
  6. php缓存小偷,小偷PHP+Html+缓存_PHP教程
  7. 教你如何快速翻译文档
  8. navicat连接oracle的错误:ora-12737:instant client light :unsupport server charater set ZHS16GBK解决办法。
  9. 建立CCS 3.3工程
  10. 更流网络seo专家分析百度快照不更新情况