Java

java怎么获取当前ip地址

小亿
94
2024-02-05 17:05:33
栏目: 编程语言

可以使用InetAddress类中的getLocalHost()方法来获取当前IP地址。代码示例如下:

import java.net.InetAddress;
import java.net.UnknownHostException;

public class GetIPAddress {
    public static void main(String[] args) {
        try {
            InetAddress address = InetAddress.getLocalHost();
            System.out.println("当前IP地址:" + address.getHostAddress());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

运行结果:

当前IP地址:192.168.0.100

0
看了该问题的人还看了