用java获取本地ip
通常,我们都是用以下代码来获取本地ip地址的
这段代码会输出计算机中所有设备的ip,找需要的用吧,呵呵//通过调用系统的PING命令,来获得局域网IP
BufferedReader read=new BufferedReader(new InputStreamReader(proc.getInputStream()));
String str="";
for (int i = 0; i < 7; i++)
str=read.readLine();
if(str.length()<17 || str.indexOf("timed out")!=-1){
//map.put(ip, new Boolean(false));
}else{
InetAddress addr=InetAddress.getByName(ip);
map.put(addr.getHostName(),ip);
} 8 楼 wushaoen 2008-05-17 /**
* @see javax.servlet.ServletRequestWrapper#getRemoteAddr()
*/
public String getRemoteAddr()
{
// We look if the request is forwarded
// If it is not call the older function.
String ip = super.getHeader("x-forwarded-for");
if (ip == null) {
ip = super.getRemoteAddr();
}
else {
// Process the IP to keep the last IP (real ip of the computer on the net)
StringTokenizer tokenizer = new StringTokenizer(ip, ",");
// Ignore all tokens, except the last one
for (int i = 0; i < tokenizer.countTokens() -1 ; i++) {
tokenizer.nextElement();
}
ip = tokenizer.nextToken().trim();
if (ip.equals("")) {
ip = null;
}
}
// If the ip is still null, we put 0.0.0.0 to avoid null values
if (ip == null) {
ip = "0.0.0.0";
}
return ip;
}
9 楼 Feiing 2008-05-18 楼上 人家都说了是获取本地 IP 了 你贴个 getRemoteAddr() 干啥