获取本机IP可区分系统可区分虚拟机和本机java程序跨平台
2013-09-12 21:44
441 查看
1、只是Windows系统使用的
InetAddress addr = InetAddress.getLocalHost();
String ip=addr.getHostAddress().toString;//获得本机IP
String address=addr.getHostName().toString;//获得本机名称
2、windows和linux都可以使用的,且只获取IPV4,返回String类型
public class GetLocalHostAddr {
public String getLocalHostAddr(){
Enumeration allNetInterfaces;
Vector<String> ipAddr = new Vector<String>();
String ipLocalAddr = null;
InetAddress ip = null;
try {
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface)
allNetInterfaces.nextElement();
System.out.println(netInterface.getName()+”====Name”);
Enumeration addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
ip = (InetAddress) addresses.nextElement();
ipAddr.add(ip.toString());
if (ip != null && ip instanceof Inet4Address) //IP是ipv4,ipv6换成Inet6Address
{
String hostAddress = ip.getHostAddress();
System.out.println("本机的IP =" + hostAddress);
System.out.println("hostAddress===="+hostAddress.equals("127.0.0.1"));
if(!hostAddress.equals("127.0.0.1") && !hostAddress.equals("/127.0.0.1")){ ipLocalAddr = ip.toString().split("[/]")[1]; //得到本地IP
}
System.out.println(ipLocalAddr+"===============ipLocalAddr");
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return ipLocalAddr;
}
3、Windows上区分虚拟机和本机的IP
判断时加上netInterface.getName()==eth0 (本机IP) netInterface.getName()==eth1 (虚拟机IP)(一个虚拟机)
4、分辨系统(通过系统名字)
String osName = System.getProperty("os.name"); //获取系统名称
if(osName!=null&&osName.startsWith("Windows")){ //如果是Windows系统
//获取IP
}esle if(osName!=null&&osName.startsWith("Linux")){ //如果是Linux系统
//获取IP }
InetAddress addr = InetAddress.getLocalHost();
String ip=addr.getHostAddress().toString;//获得本机IP
String address=addr.getHostName().toString;//获得本机名称
2、windows和linux都可以使用的,且只获取IPV4,返回String类型
public class GetLocalHostAddr {
public String getLocalHostAddr(){
Enumeration allNetInterfaces;
Vector<String> ipAddr = new Vector<String>();
String ipLocalAddr = null;
InetAddress ip = null;
try {
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface)
allNetInterfaces.nextElement();
System.out.println(netInterface.getName()+”====Name”);
Enumeration addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
ip = (InetAddress) addresses.nextElement();
ipAddr.add(ip.toString());
if (ip != null && ip instanceof Inet4Address) //IP是ipv4,ipv6换成Inet6Address
{
String hostAddress = ip.getHostAddress();
System.out.println("本机的IP =" + hostAddress);
System.out.println("hostAddress===="+hostAddress.equals("127.0.0.1"));
if(!hostAddress.equals("127.0.0.1") && !hostAddress.equals("/127.0.0.1")){ ipLocalAddr = ip.toString().split("[/]")[1]; //得到本地IP
}
System.out.println(ipLocalAddr+"===============ipLocalAddr");
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return ipLocalAddr;
}
3、Windows上区分虚拟机和本机的IP
判断时加上netInterface.getName()==eth0 (本机IP) netInterface.getName()==eth1 (虚拟机IP)(一个虚拟机)
4、分辨系统(通过系统名字)
String osName = System.getProperty("os.name"); //获取系统名称
if(osName!=null&&osName.startsWith("Windows")){ //如果是Windows系统
//获取IP
}esle if(osName!=null&&osName.startsWith("Linux")){ //如果是Linux系统
//获取IP }
相关文章推荐
- java获取系统语言(区分简体中文和繁体中文)
- linux 系统获取网络ip, mask, gateway, dns信息小程序
- JAVA程序获取本机ip
- java获取本机ip(排除虚拟机等一些ip)
- 4000 java获取程序运行环境的所有系统属性
- java 获取本地系统IP 及网咯IP
- 【分享】Java程序获取本机ip,mac,os名称,版本等
- java获取系统语言(区分简体中文和繁体中文) .
- 用java获取并传出虚拟机系统实时性能参数(1:得到性能参数)
- 关于手动设置ip和自动获取ip的java小程序
- JAVA/C/C++获取当前系统毫秒时间+计算程序运行时间
- windows和linux下执行java程序+获取本机IP
- java 如何获取多网卡时的本机IP
- 获取java程序的系统参数
- java程序获取IP、MAC相关地址属性
- java工具类,在Windows,Linux系统获取电脑的MAC地址、本地IP、电脑名
- java获取系统信息:java的信息、操作系统的信息、用户的信息、虚拟机的信息、系统设置的信息。
- Linux 中java程序获取系统时间跟Linux服务器时间不一致问题
- Java程序获取本机ip和主机名
- java获取系统网络端口IP