更新時間:2022-05-31 09:22:08 來源:動力節(jié)點 瀏覽3141次
動力節(jié)點小編告訴大家,在 Java 中,您可以使用 InetAddress.getLocalHost() 獲取當(dāng)前運行 Java 應(yīng)用程序的服務(wù)器的 IP 地址和獲取InetAddress.getHostName()當(dāng)前服務(wù)器名稱的主機(jī)名。
package com.crunchify.tutorials;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @author Crunchify.com
*/
public class CrunchifyGetIPHostname {
public static void main(String[] args) {
InetAddress ip;
String hostname;
try {
ip = InetAddress.getLocalHost();
hostname = ip.getHostName();
System.out.println("Your current IP address : " + ip);
System.out.println("Your current Hostname : " + hostname);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
輸出:
Your current IP address : appshah-mac/192.168.0.1
Your current Hostname : appshah-mac
相關(guān)閱讀