首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

用java获取当地ip

2012-10-28 
用java获取本地ip通常,我们都是用以下代码来获取本地ip地址的这段代码会输出计算机中所有设备的ip,找需要

用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() 干啥

热点排行