當前位置:首頁 » 雲伺服器 » java如何獲取本伺服器ip地址

java如何獲取本伺服器ip地址

發布時間: 2025-08-11 10:31:01

㈠ 如何在java中獲取本地ip

很多朋友都想知道java如何獲取本地ip?下面就一起來了解一下吧~

獲取java本地ip一共有兩種方法:1、inetAddress類;2、封裝方法。

1、 inetAddress類

通過InetAddress的實例對象包含以數字形式保存的IP地址,同時還可能包含主機名(如果使用主機名來獲取InetAddress的實例,或者使用數字來構造,並且啟用了反向主機名解析的功能)。InetAddress類提供了將主機名解析為IP地址(或反之)的方法。其生成InetAddress對象的方法。

import java.net.Inet4Address; import java.net.InetAddress; import java.net.UnknownHostException; public class Main {     public static void main(String[] args) throws UnknownHostException {         //Inet4Address address= (Inet4Address) Inet4Address.getLocalHost();         InetAddress address = InetAddress.getLocalHost();         System.out.println(address);//獲取計算機名稱和ip地址         String hostAddress = address.getHostAddress();         System.out.println(hostAddress);//獲取ip地址         String hostName = address.getHostName();         System.out.println(hostName);//獲取計算機名稱     } }

2、封裝方法。

    public static String getLocalIp() {         Enumeration  netInterfaces = null;         try {             netInterfaces = NetworkInterface.getNetworkInterfaces();             while (netInterfaces.hasMoreElements()) {                 NetworkInterface nif = netInterfaces.nextElement();                 Enumeration  InetAddress = nif.getInetAddresses();                 while (InetAddress.hasMoreElements()) {                     String ip = InetAddress.nextElement().getHostAddress();                     if (ip.startsWith("192.168")) {                         return ip;                     }                 }             }         } catch (SocketException e) {         }         return "127.0.0.1";     }

㈡ Java獲取主機的基本信息

在Java中獲取主機的基本信息,可以按照以下步驟進行:

獲取主機名稱和系統類型主機名稱:使用InetAddress類的getHostName方法獲取。 系統類型:使用System類的getProperty方法,傳入"os.name"等屬性名來獲取系統類型和用戶信息。

獲取IP地址: 使用InetAddress類的getAllByName方法獲取所有與主機名對應的IP地址。 注意,Windows系統與非Windows系統在獲取IP地址時可能有所不同,非Windows系統通常能更准確地獲取到網卡對應的IP。

獲取CPU信息: 使用oshi庫來獲取CPU的詳細信息,包括物理可用核數和CPU使用率等。 使用oshi庫前,需要導入JNA和jnaplatform庫。

獲取內存信息: 通過OperatingSystemMXBean類獲取主機的物理內存總量和可用內存,從而計算內存使用率。 使用oshi包中的SystemInfo類也可以獲取內存信息。 使用MemoryMXBean介面中的MemoryUsage類獲取JVM的內存信息,包括初始內存大小、最大可用內存及當前已使用內存。

獲取磁碟信息: 使用File類遍歷主機的所有磁碟,獲取每個磁碟的使用情況。 通過計算每個磁碟的已使用空間和總空間,可以得到磁碟的使用率。 匯總所有磁碟的信息,可以得到主機的總磁碟使用率。

以上方法可以幫助你在Java程序中獲取主機的各種基本信息,從而進行資源監控和管理。

㈢ 如何用Java和jsp獲取伺服器ip

<%
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
out.print(ip);
%>

㈣ java中獲取本地IP地址

方法如下:
方法一,使用CMD命令:
public static String getLocalIPForCMD(){
StringBuilder sb = new StringBuilder();
String command = "cmd.exe /c ipconfig | findstr IPv4";
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while((line = br.readLine()) != null){
line = line.substring(line.lastIndexOf(":")+2,line.length());
sb.append(line);
}
br.close();
p.destroy();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
方法二,使用Java方法:
public static String getLocalIPForJava(){
StringBuilder sb = new StringBuilder();
try {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface intf = (NetworkInterface) en.nextElement();
Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
while (enumIpAddr.hasMoreElements()) {
InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()
&& inetAddress.isSiteLocalAddress()) {
sb.append(inetAddress.getHostAddress().toString()+"\n");
}
}
}
} catch (SocketException e) { }
return sb.toString();
}

熱點內容
國內編程交流 發布:2025-08-11 14:25:46 瀏覽:430
供料站編程 發布:2025-08-11 14:19:48 瀏覽:57
matlab的編程語言 發布:2025-08-11 14:10:15 瀏覽:618
回溯演算法題 發布:2025-08-11 14:00:54 瀏覽:654
sqlserver恢復備份 發布:2025-08-11 14:00:13 瀏覽:968
家裡網路密碼如何查看二維碼 發布:2025-08-11 13:54:30 瀏覽:436
sql文件怎麼保存數據 發布:2025-08-11 13:19:25 瀏覽:742
python排列組合函數 發布:2025-08-11 12:54:03 瀏覽:973
ipfs自主可控分布式存儲 發布:2025-08-11 12:53:17 瀏覽:473
yy許可權密碼忘記了什麼辦 發布:2025-08-11 12:48:16 瀏覽:838