站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2EE 专题寻求--JAVA--取服务器IP地址方法

寻求--JAVA--取服务器IP地址方法

摘要: 寻求--JAVA--取服务器IP地址方法


<%@ page contentType="text/html; charset=GBK" errorPage="error.jsp"%>
<%@ page import="java.net.*"%>
<%@ page import="java.io.*"%>
<%
try {
out.println("Operating System: " + System.getProperty("os.name")+"
");
out.println("IP/Localhost: " + InetAddress.getLocalHost().getHostAddress()+"
");
out.println("Hostname:"+InetAddress.getLocalHost().getHostName()+"
");

// String name = "localhost";
// InetAddress[] addresses = InetAddress.getAllByName(name);
// for(int i=0; i// out.println(name+"["+i+"] : "+addresses[i].getHostAddress()+"
");
// }
} catch(Throwable ex) {
ex.printStackTrace();
}
%>
/////////////////////////////////
以上代码在WINDOWS下取IP没有问题。但是在LINUX取的时候只取到127.0.0.1,没有取到真实的IP地址。请大家帮忙。。

/////////以下是LINUX网卡信息
eth0 Link encap:Ethernet HWaddr 00:E0:4C:FC:CC:72
inet addr:192.168.0.200 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::2e0:4cff:fefc:cc72/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1568659 errors:0 dropped:0 overruns:0 frame:0
TX packets:1345045 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1621164713 (1.5 GiB) TX bytes:967412057 (922.5 MiB)
Interrupt:10 Base address:0xc000

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:2166 errors:0 dropped:0 overruns:0 frame:0
TX packets:2166 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1861144 (1.7 MiB) TX bytes:1861144 (1.7 MiB)



怎么没有人来回答呀


我也遇到同样的问题,关注


有兄弟们会,请不吝赐教


private String getLocalSiteIP() throws Exception {
String siteString = "";
Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
InetAddress ip = (InetAddress) ni.getInetAddresses().nextElement();
if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() &&
ip.getHostAddress().indexOf(":") == -1) {
System.out.println("Interface " + ni.getName() +
" seems to be InternetInterface. I'll take it...");
siteString = ip.getHostAddress();
}
}
return siteString;
}

对单网卡有用,windows/linux都调通,希望对你有用,多网卡还是让用户配置吧


楼上的兄弟。我在redhat as4上测试还是取不到


最后只能修改/etc/hosts


↑返回目录
前一篇: hibernate查询问题,报org.hibernate.hql.ast.QuerySyntaxException异常?
后一篇: 求助:jsp中tomcat的sql server2000数据库连接池的问题