首页 > 开发 > Java > 正文

java编程实现获取服务器IP地址及MAC地址的方法

2024-07-13 09:56:10
字体:
来源:转载
供稿:网友

这篇文章主要介绍了java编程实现获取机器IP地址及MAC地址的方法,实例分析了Java分别针对单网卡及多网卡的情况下获取服务器IP地址与MAC地址的相关技巧,需要的朋友可以参考下

本文实例讲述了java编程实现获取服务器IP地址及MAC地址的方法。分享给大家供大家参考,具体如下:

已测系统:

windows linux unix

排除127.0.0.1 和 0.0.0.0.1等非正常IP

 

 
  1. import java.net.InetAddress; 
  2. import java.net.NetworkInterface; 
  3. import java.net.SocketException; 
  4. import java.util.ArrayList; 
  5. import java.util.Enumeration; 
  6. import java.util.List; 
  7. public class IpUtil { 
  8. private IpUtil(){} 
  9. /** 
  10. * 此方法描述的是:获得服务器的IP地址 
  11. * @author: zhangyang33@sinopharm.com 
  12. * @version: 2014年9月5日 下午4:57:15 
  13. */ 
  14. public static String getLocalIP() { 
  15. String sIP = ""
  16. InetAddress ip = null
  17. try { 
  18. boolean bFindIP = false
  19. Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface 
  20. .getNetworkInterfaces(); 
  21. while (netInterfaces.hasMoreElements()) { 
  22. if (bFindIP) { 
  23. break
  24. NetworkInterface ni = (NetworkInterface) netInterfaces 
  25. .nextElement(); 
  26. Enumeration<InetAddress> ips = ni.getInetAddresses(); 
  27. while (ips.hasMoreElements()) { 
  28. ip = (InetAddress) ips.nextElement(); 
  29. if (!ip.isLoopbackAddress()  
  30. && ip.getHostAddress().matches( 
  31. "(//d{1,3}//.){3}//d{1,3}")) { 
  32. bFindIP = true
  33. break
  34. catch (Exception e) { 
  35. OutUtil.error(IpUtil.class, e.getMessage()); 
  36. if (null != ip) { 
  37. sIP = ip.getHostAddress(); 
  38. return sIP; 
  39. /** 
  40. * 此方法描述的是:获得服务器的IP地址(多网卡) 
  41. * @author: zhangyang33@sinopharm.com 
  42. * @version: 2014年9月5日 下午4:57:15 
  43. */ 
  44. public static List<String> getLocalIPS() { 
  45. InetAddress ip = null
  46. List<String> ipList = new ArrayList<String>(); 
  47. try { 
  48. Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface 
  49. .getNetworkInterfaces(); 
  50. while (netInterfaces.hasMoreElements()) { 
  51. NetworkInterface ni = (NetworkInterface) netInterfaces 
  52. .nextElement(); 
  53. Enumeration<InetAddress> ips = ni.getInetAddresses(); 
  54. while (ips.hasMoreElements()) { 
  55. ip = (InetAddress) ips.nextElement(); 
  56. if (!ip.isLoopbackAddress()  
  57. && ip.getHostAddress().matches( 
  58. "(//d{1,3}//.){3}//d{1,3}")) { 
  59. ipList.add(ip.getHostAddress()); 
  60. catch (Exception e) { 
  61. OutUtil.error(IpUtil.class, e.getMessage()); 
  62. return ipList; 
  63. /** 
  64. * 此方法描述的是:获得服务器的MAC地址 
  65. * @author: zhangyang33@sinopharm.com 
  66. * @version: 2014年9月5日 下午1:27:25 
  67. */ 
  68. public static String getMacId() { 
  69. String macId = ""
  70. InetAddress ip = null
  71. NetworkInterface ni = null
  72. try { 
  73. boolean bFindIP = false
  74. Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface 
  75. .getNetworkInterfaces(); 
  76. while (netInterfaces.hasMoreElements()) { 
  77. if (bFindIP) { 
  78. break
  79. ni = (NetworkInterface) netInterfaces 
  80. .nextElement(); 
  81. // ----------特定情况,可以考虑用ni.getName判断 
  82. // 遍历所有ip 
  83. Enumeration<InetAddress> ips = ni.getInetAddresses(); 
  84. while (ips.hasMoreElements()) { 
  85. ip = (InetAddress) ips.nextElement(); 
  86. if (!ip.isLoopbackAddress() // 非127.0.0.1 
  87. && ip.getHostAddress().matches( 
  88. "(//d{1,3}//.){3}//d{1,3}")) { 
  89. bFindIP = true
  90. break
  91. catch (Exception e) { 
  92. OutUtil.error(IpUtil.class, e.getMessage()); 
  93. if (null != ip) { 
  94. try { 
  95. macId = getMacFromBytes(ni.getHardwareAddress()); 
  96. catch (SocketException e) { 
  97. OutUtil.error(IpUtil.class, e.getMessage()); 
  98. return macId; 
  99. /** 
  100. * 此方法描述的是:获得服务器的MAC地址(多网卡) 
  101. * @author: zhangyang33@sinopharm.com 
  102. * @version: 2014年9月5日 下午1:27:25 
  103. */ 
  104. public static List<String> getMacIds() { 
  105. InetAddress ip = null
  106. NetworkInterface ni = null
  107. List<String> macList = new ArrayList<String>(); 
  108. try { 
  109. Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface 
  110. .getNetworkInterfaces(); 
  111. while (netInterfaces.hasMoreElements()) { 
  112. ni = (NetworkInterface) netInterfaces 
  113. .nextElement(); 
  114. // ----------特定情况,可以考虑用ni.getName判断 
  115. // 遍历所有ip 
  116. Enumeration<InetAddress> ips = ni.getInetAddresses(); 
  117. while (ips.hasMoreElements()) { 
  118. ip = (InetAddress) ips.nextElement(); 
  119. if (!ip.isLoopbackAddress() // 非127.0.0.1 
  120. && ip.getHostAddress().matches( 
  121. "(//d{1,3}//.){3}//d{1,3}")) { 
  122. macList.add(getMacFromBytes(ni.getHardwareAddress())); 
  123. catch (Exception e) { 
  124. OutUtil.error(IpUtil.class, e.getMessage()); 
  125. return macList; 
  126. private static String getMacFromBytes(byte[] bytes) { 
  127. StringBuffer mac = new StringBuffer(); 
  128. byte currentByte; 
  129. boolean first = false
  130. for (byte b : bytes) { 
  131. if (first) { 
  132. mac.append("-"); 
  133. currentByte = (byte) ((b & 240) >> 4); 
  134. mac.append(Integer.toHexString(currentByte)); 
  135. currentByte = (byte) (b & 15); 
  136. mac.append(Integer.toHexString(currentByte)); 
  137. first = true
  138. return mac.toString().toUpperCase(); 

希望本文所述对大家Java程序设计有所帮助。


注:相关教程知识阅读请移步到JAVA教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表