java如何获取本服务器ip地址
㈠ 如何在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();
}