Java 工具类 IpUtil - 获取本机所有 IP 地址,LocalHost 对应地址 IP

你的名字 2021-12-01 12:52 838阅读 0赞

Java 工具类 IpUtil - 获取本机所有 IP 地址,LocalHost 对应地址 IP

IP 工具类

源代码:

  1. /** * <p> * * @author XiaoPengwei * @since 2019-07-20 */
  2. import java.net.Inet4Address;
  3. import java.net.InetAddress;
  4. import java.net.NetworkInterface;
  5. import java.net.SocketException;
  6. import java.net.UnknownHostException;
  7. import java.util.Enumeration;
  8. public class IpUtil {
  9. public static void main(String[] args) {
  10. IpUtil testGetIP = new IpUtil();
  11. System.out.println("Your LocalHost IP(ipv4) is:");
  12. System.out.println(testGetIP.getIP());
  13. System.out.println("\nHere are all your IPS:");
  14. testGetIP.printAllIp();
  15. }
  16. /** * Fond LocalHost ipv4 * * @return java.lang.String */
  17. public String getIP() {
  18. try {
  19. // 根据 hostname 找 ip
  20. InetAddress address = InetAddress.getLocalHost();
  21. if (address.isLoopbackAddress()) {
  22. Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
  23. while (allNetInterfaces.hasMoreElements()) {
  24. NetworkInterface netInterface = allNetInterfaces.nextElement();
  25. Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
  26. while (addresses.hasMoreElements()) {
  27. InetAddress ip = addresses.nextElement();
  28. if (!ip.isLinkLocalAddress() && !ip.isLoopbackAddress() && ip instanceof Inet4Address) {
  29. return ip.getHostAddress();
  30. }
  31. }
  32. }
  33. }
  34. return address.getHostAddress();
  35. } catch (UnknownHostException | SocketException e) {
  36. e.printStackTrace();
  37. return null;
  38. }
  39. }
  40. /** * Find all network interfaces */
  41. public void printAllIp() {
  42. try {
  43. Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
  44. while (allNetInterfaces.hasMoreElements()) {
  45. NetworkInterface netInterface = allNetInterfaces.nextElement();
  46. // Remove loopback interface, sub-interface, unrun and interface
  47. if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
  48. continue;
  49. }
  50. Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
  51. while (addresses.hasMoreElements()) {
  52. InetAddress ip = addresses.nextElement();
  53. if (ip != null) {
  54. System.out.println("ip = " + ip.getHostAddress());
  55. // ipv4
  56. if (ip instanceof Inet4Address) {
  57. System.out.println("ipv4 = " + ip.getHostAddress());
  58. if (!ip.getHostAddress().startsWith("192") && !ip.getHostAddress().startsWith("10") && !ip.getHostAddress().startsWith("172")) {
  59. // Intranet
  60. ip.getHostAddress();
  61. }
  62. }
  63. }
  64. }
  65. }
  66. } catch (SocketException e) {
  67. System.err.println("[Error] can't get host ip address" + e.getMessage());
  68. }
  69. }
  70. }

发表评论

表情:
评论列表 (有 0 条评论,838人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java获取所有IP地址

    在网上找了几个用java获取本机IP地址的代码,发现都少都有些不完美,自己整理了一下\[@more@\]突然之间很想把自己的IP地址给获取了,虽然用系统自带命令可以得到,但自己

    相关 java获取IP地址

    在网上找了几个用java获取本机IP地址的代码,发现都少都有些不完美,自己整理了一下.突然之间很想把自己的IP地址给获取了,虽然用系统自带命令可以得到,但自己想写一个程序获取一