因为项目中正好用到本地IP地址,然后上网搜了一下,看写的不是特别详细,所以在这里补充一下。
首先获取到本地设备的名称和IP

InetAddress inetAddress = InetAddress.getLocalHost();

然后输出为:

Q4ER6ZHVM5NWJ2X/192.168.2.23

如果要用到程序中,则需要将这个转换为String类型,并且截取到IP,因为我们只要IP地址

String ip = inetAddress.toString();
String localhost = StringUtils.substringAfterLast(ip, "/");

这个时候,输出的结果就是我们想要的了

//String类型的
192.168.2.23

其中的StringUtils.substringAfterLast这个的意思是截取”/”后面的字符串。
1、StringUtils工具类的具体用法。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-27
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-14
  • 2022-01-14
  • 2021-12-03
  • 2021-09-29
相关资源
相似解决方案