如何用 Java 获取系统 IP
答案:2 悬赏:50
解决时间 2021-01-26 03:56
- 提问者网友:动次大次蹦擦擦
- 2021-01-25 10:18
如何用 Java 获取系统 IP
最佳答案
- 二级知识专家网友:duile
- 2021-01-25 11:13
public static InetAddress getSystemLocalIp() throws UnknownHostException{
InetAddress inet=null;
String osname=getSystemOSName();
try {
//针对window系统
if(osname.equalsIgnoreCase("Windows XP")){
inet=getWinLocalIp();
//针对linux系统
}else if(osname.equalsIgnoreCase("Linux")){
inet=getUnixLocalIp();
}
if(null==inet){
throw new UnknownHostException("主机的ip地址未知");
}
}catch (SocketException e) {
logger.error("获取本机ip错误"+e.getMessage());
throw new UnknownHostException("获取本机ip错误"+e.getMessage());
}
return inet;
}
InetAddress inet=null;
String osname=getSystemOSName();
try {
//针对window系统
if(osname.equalsIgnoreCase("Windows XP")){
inet=getWinLocalIp();
//针对linux系统
}else if(osname.equalsIgnoreCase("Linux")){
inet=getUnixLocalIp();
}
if(null==inet){
throw new UnknownHostException("主机的ip地址未知");
}
}catch (SocketException e) {
logger.error("获取本机ip错误"+e.getMessage());
throw new UnknownHostException("获取本机ip错误"+e.getMessage());
}
return inet;
}
全部回答
- 1楼网友:慢性怪人
- 2021-01-25 12:01
import java.net.*;
public class Test6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
InetAddress ia=null;
try {
ia=ia.getLocalHost();
String localname=ia.getHostName();
String localip=ia.getHostAddress();
System.out.println("本机名称是:"+ localname);
System.out.println("本机的ip是 :"+localip);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Test6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
InetAddress ia=null;
try {
ia=ia.getLocalHost();
String localname=ia.getHostName();
String localip=ia.getHostAddress();
System.out.println("本机名称是:"+ localname);
System.out.println("本机的ip是 :"+localip);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯