如何在 Java 中检查系统 IP 地址/主机名?

发布于 2024-10-08 22:19:59 字数 67 浏览 0 评论 0原文

有没有办法在不使用外部进程的情况下找出系统IP地址? 我想为我的应用程序获取这些信息,但如果可能的话,使用纯java。

is there any way to find out system IP Address without using external process?
I want to grab this information for my application but in pure java if possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

差↓一点笑了 2024-10-15 22:19:59

这满足您的需求吗?

import java.net.*;
import java.io.*;
import java.applet.*;

public class GetClientIP extends Applet {
  public void init() {
    try {
     InetAddress thisIp =
        InetAddress.getLocalHost();
     System.out.println("IP:"+thisIp.getHostAddress());
     }
    catch(Exception e) {
     e.printStackTrace();
     }
    }
}

Does this meet your needs?

import java.net.*;
import java.io.*;
import java.applet.*;

public class GetClientIP extends Applet {
  public void init() {
    try {
     InetAddress thisIp =
        InetAddress.getLocalHost();
     System.out.println("IP:"+thisIp.getHostAddress());
     }
    catch(Exception e) {
     e.printStackTrace();
     }
    }
}
心房敞 2024-10-15 22:19:59

InetAddress.getLocalHost().getHostAddress() 调用并不总是有效;有时它会返回 127.0.0.1。

请参阅 java InetAddress.getLocalHost( );返回 127.0.0.1 ...如何获取真实 IP? 了解更多详细信息和其他选项。

The InetAddress.getLocalHost().getHostAddress() call doesn't always work; sometimes it returns 127.0.0.1.

See java InetAddress.getLocalHost(); returns 127.0.0.1 ... how to get REAL IP? for some more details and other options.

幻想少年梦 2024-10-15 22:19:59

您好,是的,通过 InetAdres 类可以实现。检查此链接 JAVA API 和您需要的方法是 getHostAddress()

Hi yes its possible with the class InetAddres. Check this link JAVA API and the method you need is getHostAddress()

嘦怹 2024-10-15 22:19:59

考虑使用 NetworkInterface 类。

Consider using the NetworkInterface class.

蘸点软妹酱 2024-10-15 22:19:59

如果你想在java应用程序中查找ip地址

 InetAddress localIP=InetAddress.getLocalHost();

并且如果你正在使用web应用程序

  request.getRemoteAddr();

If you want to find ip address in java application

 InetAddress localIP=InetAddress.getLocalHost();

and if you are using web application

  request.getRemoteAddr();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文