惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
N
News | PayPal Newsroom
S
Security @ Cisco Blogs
Schneier on Security
Schneier on Security
V
V2EX - 技术
S
Secure Thoughts
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
S
Securelist
S
Security Archives - TechRepublic
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
Security Latest
Security Latest
Recent Commits to openclaw:main
Recent Commits to openclaw:main
G
GRAHAM CLULEY
H
Hacker News: Front Page
Microsoft Azure Blog
Microsoft Azure Blog
I
Intezer
Google Online Security Blog
Google Online Security Blog
美团技术团队
阮一峰的网络日志
阮一峰的网络日志
T
The Exploit Database - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Webroot Blog
Webroot Blog
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
P
Proofpoint News Feed
The Cloudflare Blog
I
InfoQ
L
LangChain Blog
U
Unit 42
P
Proofpoint News Feed
S
Schneier on Security
S
Security Affairs
Y
Y Combinator Blog
T
Tenable Blog
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
量子位
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
博客园 - 聂微东
D
Darknet – Hacking Tools, Hacker News & Cyber Security
GbyAI
GbyAI
AWS News Blog
AWS News Blog

博客园 - 星朝

博客园搬家 窗口函数详细用法 JVM启动参数大全 BigDecimal.setScale用法总结 - 星朝 - 博客园 springboot项目上传文件出现临时文件目录为空 springboot中多线程中使用MultipartFile进行异步操作报错,系统找不到指定的文件 Java获取本机ip和服务器ip Spring中的@Transactional(rollbackFor = Exception.class)属性详解 注解@CrossOrigin解决跨域问题 unique key index区别 mysql 创建唯一约束表 MySQL中添加唯一约束和联合唯一约束 移动IM开源框架Tigase > Openfire > Ejabberd对比分析 Tigase 8.0开发环境搭建 Tigase手动安装过程 几个时序数据库 时序数据库入门 MySQL执行计划extra中的using index 和 using where using index 的区别 ETL的详细解释定义
java获取本机IP
星朝 · 2020-01-14 · via 博客园 - 星朝

如果是在windows环境: 使用InetAddress.getLocalHost()方法即可.

  1. import java.net.InetAddress;

  2. public class Main {

  3. public static void main(String[] args)

  4. throws Exception {

  5. InetAddress addr = InetAddress.getLocalHost();

  6. System.out.println("Local HostAddress:

  7. "+addr.getHostAddress());

  8. String hostname = addr.getHostName();

  9. System.out.println("Local host name: "+hostname);

  10. }

  11. }

代码运行结果:

  1. Local HostAddress: 192.168.42.2

  2. Local host name: f19ca2b695da

在linux下上述获取IP的方式有时候会得到127.0.0.1.

从JDK1.4开始,Java提供了一个NetworkInterface类。这个类可以得到本机所有的物理网络接口和虚拟机等软件利用本机的物理网络接口创建的逻辑网络接口的信息,NetworkInterface可以通过getNetworkInterfaces方法来枚举本机所有的网络接口。我们也可以利用getNetworkInterfaces得到的网络接口来枚举本机的所有IP地址。当然,也可以通过InetAddress类的getAllByName来得到本机的所有IP地址:

public static Enumeration<NetworkInterface> getNetworkInterfaces() throws SocketException

但getNetworkInterfaces方法可以按网络接口将这些IP地址进行分组,这对于只想得到某个网络接口上的所有IP地址是非常有用的。NetworkInterface类提供了三个方法可以分别得到网络接口名(getName方法)、网络接口别名(getDisplayName方法)以及和网络接口绑定的所有IP地址(getInetAddresses方法):

1. getName方法

这个方法用来得到一个网络接口的名称。这个名称就是使用getByName方法创建NetworkInterface对象时使用的网络接口名,如eth0ppp0等。getName方法的定义如下:

public String getName()

2. getDisplayName方法

这个方法可以得到更容易理解的网络接口名,也可以将这个网络接口名称为网络接口别名。在一些操作系统中(如Unix),getDisplayName方法和getName方法的返回值相同,但在WindowsgetDisplayName方法一般会返回一个更为友好的名字,如Realtek RTL8139 Family PCI Fast Ethernet NICgetDisplayName方法的定义如下:


public String getDisplayName()

3. getInetAddresses方法

NetworkInterface类可以通过getInetAddresse方法以InetAddress对象的形式返回和网络接口绑定的所有IP地址。getInetAddresses方法的定义如下:

public Enumeration<InetAddress> getInetAddresses()

下面给出windows和linux下通用的获取本机IP的方法:

  1. import java.net.Inet4Address;

  2. import java.net.InetAddress;

  3. import java.net.NetworkInterface;

  4. import java.util.Enumeration;

  5. public class Main {

  6. public static void main(String[] args) {

  7. System.out.println("本机IP:" + getIpAddress());

  8. }

  9. public static String getIpAddress() {

  10. try {

  11. Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();

  12. InetAddress ip = null;

  13. while (allNetInterfaces.hasMoreElements()) {

  14. NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();

  15. if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {

  16. continue;

  17. } else {

  18. Enumeration<InetAddress> addresses = netInterface.getInetAddresses();

  19. while (addresses.hasMoreElements()) {

  20. ip = addresses.nextElement();

  21. if (ip != null && ip instanceof Inet4Address) {

  22. return ip.getHostAddress();

  23. }

  24. }

  25. }

  26. }

  27. } catch (Exception e) {

  28. System.err.println("IP地址获取失败" + e.toString());

  29. }

  30. return "";

  31. }

  32. }

表示对网络接口进行筛选,非回送接口 且 非虚拟网卡 且 正在使用中

注:

netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp() 用于排除回送接口,非虚拟网卡,未在使用中的网络接口.

本文参考:

http://www.runoob.com/java/net-localip.html

https://www.oschina.net/question/129471_39474

http://blog.51cto.com/androidguy/214458

原文地址:https://blog.csdn.net/nianbingsihan/article/details/80265029