当前位置:   article > 正文

Java获取本机IP地址的方法(内网、公网)

java获取本机ip

起因是公司一个springboot项目启动类打印了本机IP地址加端口号,方便访问项目页面,但是发现打印出来的不是“无线局域网”的ip而是“以太网适配器”ip,如下图所示

这样就导致后续本地起项目连接xxl-job注册节点的时候因为不在同个局域网下ping不通出问题,所以需要解决一下。

一、直接获取本机IP(会受到虚拟机干扰)

  1. public static String getInterIP1() throws Exception {
  2. return InetAddress.getLocalHost().getHostAddress();
  3. }

二、获取本机IP(排除虚拟机干扰)

  1. public static InetAddress getLocalHostLANAddress() throws UnknownHostException {
  2. try {
  3. InetAddress candidateAddress = null;
  4. // 遍历所有的网络接口
  5. for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {
  6. NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
  7. // 在所有的接口下再遍历IP
  8. for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
  9. InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
  10. if (!inetAddr.isLoopbackAddress()) {// 排除loopback类型地址
  11. if (inetAddr.isSiteLocalAddress()) {
  12. // 如果是site-local地址,就是它了
  13. return inetAddr;
  14. } else if (candidateAddress == null) {
  15. // site-local类型的地址未被发现,先记录候选地址
  16. candidateAddress = inetAddr;
  17. }
  18. }
  19. }
  20. }
  21. if (candidateAddress != null) {
  22. return candidateAddress;
  23. }
  24. // 如果没有发现 non-loopback地址.只能用最次选的方案
  25. InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
  26. if (jdkSuppliedAddress == null) {
  27. throw new UnknownHostException("The JDK InetAddress.getLocalHost() method unexpectedly returned null.");
  28. }
  29. return jdkSuppliedAddress;
  30. } catch (Exception e) {
  31. UnknownHostException unknownHostException = new UnknownHostException(
  32. "Failed to determine LAN address: " + e);
  33. unknownHostException.initCause(e);
  34. throw unknownHostException;
  35. }
  36. }

三、获取本机公网IP

  1. public static String getOutIP() {
  2. try {
  3. URL whatismyip = new URL("http://checkip.amazonaws.com");
  4. BufferedReader in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
  5. String ip = in.readLine();
  6. return ip;
  7. } catch (Exception e) {}
  8. return "";
  9. }

四、测试

  1. public class IpTest {
  2. public static void main(String[] args) throws Exception {
  3. System.out.println("获取本机ip: " + getInterIP1());
  4. // System.out.println("getInterIP2: " + getInterIP2());
  5. System.out.println("获取本机ip(排除虚拟机干扰): " + String.valueOf(getLocalHostLANAddress()).substring(1));
  6. // System.out.println("getOutIPV4: " + getOutIPV4());
  7. System.out.println("获取本机公网ip: " + getOutIP());
  8. }
  9. public static String getInterIP1() throws Exception {
  10. return InetAddress.getLocalHost().getHostAddress();
  11. }
  12. public static InetAddress getLocalHostLANAddress() throws UnknownHostException {
  13. try {
  14. InetAddress candidateAddress = null;
  15. // 遍历所有的网络接口
  16. for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {
  17. NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
  18. // 在所有的接口下再遍历IP
  19. for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
  20. InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
  21. if (!inetAddr.isLoopbackAddress()) {// 排除loopback类型地址
  22. if (inetAddr.isSiteLocalAddress()) {
  23. // 如果是site-local地址,就是它了
  24. return inetAddr;
  25. } else if (candidateAddress == null) {
  26. // site-local类型的地址未被发现,先记录候选地址
  27. candidateAddress = inetAddr;
  28. }
  29. }
  30. }
  31. }
  32. if (candidateAddress != null) {
  33. return candidateAddress;
  34. }
  35. // 如果没有发现 non-loopback地址.只能用最次选的方案
  36. InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
  37. if (jdkSuppliedAddress == null) {
  38. throw new UnknownHostException("The JDK InetAddress.getLocalHost() method unexpectedly returned null.");
  39. }
  40. return jdkSuppliedAddress;
  41. } catch (Exception e) {
  42. UnknownHostException unknownHostException = new UnknownHostException(
  43. "Failed to determine LAN address: " + e);
  44. unknownHostException.initCause(e);
  45. throw unknownHostException;
  46. }
  47. }
  48. public static String getOutIP() {
  49. try {
  50. URL whatismyip = new URL("http://checkip.amazonaws.com");
  51. BufferedReader in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
  52. String ip = in.readLine();
  53. return ip;
  54. // System.out.println("Public IP Address: " + ip);
  55. } catch (Exception e) {
  56. // System.out.println("Error occurred: " + e.getMessage());
  57. }
  58. return "";
  59. }
  60. }
  1. 获取本机ip: 172.17.16.1
  2. 获取本机ip(排除虚拟机干扰): 192.168.100.100
  3. 获取本机公网ip: 14.145.43.147

这里打印出来的便对应上前言命令行截图里的ip信息,然后公网ip对应的是百度搜索ip查到的公网ip地址 

五、手动禁用虚拟机排除干扰

除了用代码逻辑排除虚拟机干扰,我们也可以直接禁用电脑的虚拟机适配器。

步骤:

  1. win+R 
  2. devmgmt.msc 打开设备管理器
  3. 找到网络适配器-Hyper-V Virtual 开头的  右键禁用

此时win+R cmd 输入ipconfig,可以看到已经没有虚拟机ip了

此时java使用

InetAddress.getLocalHost().getHostAddress()

也可以获取到192.168开头的wifi地址了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/992806
推荐阅读
相关标签
  

闽ICP备14008679号