JAVA获取IP地址以及对应的地区

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import com.weimingfj.common.utils.JsonUtil;

public class Util {


     @SuppressWarnings({ "unchecked" })
     public static String[] getIpAreaCode(String ipAddr){
           Map<String,String> ipParameter= new HashMap<String, String>();
           ipParameter.put( "ip", ipAddr);
           String jsonStr= "";
           String[] area= new String[2];
            try {
                jsonStr = HttpUtil. post(ipParameter, "http://ip.taobao.com/service/getIpInfo.php" );
                Map<String,Object> ipResultMap=JsonUtil.jsonToMapObject(jsonStr);
                 if( "0.0".equals(ipResultMap.get( "code").toString())){
                     Map<String,String> data=(Map<String,String>)ipResultMap.get("data" );
                      if(! "-1".equals(data.get( "county_id"))){
                           area[0]=data.get( "county_id");
                          area[1]=data.get( "region")+data.get( "city")+data.get( "county");
                            return area;
                     }
                      if(! "-1".equals(data.get( "city_id"))){
                           area[0]=data.get( "city_id");
                          area[1]=data.get( "region")+data.get( "city");
                            return area;
                     }
                      if(! "-1".equals(data.get( "region_id"))){
                           area[0]=data.get( "region_id");
                           area[1]=data.get( "region");
                            return area;
                     }
                }
           } catch (Exception e) {
                e.printStackTrace();
           }
            return area;
     }
     
     public static String getIP(HttpServletRequest request) {
           String ip = request.getHeader( "x-forwarded-for");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader( "Proxy-Client-IP");
           }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader( "WL-Proxy-Client-IP");
           }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
                 if(ip.equals( "127.0.0.1") || ip.equals("0:0:0:0:0:0:0:1" )){ 
                //根据网卡取本机配置的IP 
                InetAddress inet= null; 
                try { 
                    inet = InetAddress. getLocalHost(); 
                } catch (UnknownHostException e) { 
                    e.printStackTrace(); 
                } 
                ip= inet.getHostAddress();
                String reg = "(10|172|192)\\.([0-1][0-9]{0,2}|[2][0-5]{0,2}|[3-9][0-9]{0,1})\\.([0-1][0-9]{0,2}|[2][0-5]{0,2}|[3-9][0-9]{0,1})\\.([0-1][0-9]{0,2}|[2][0-5]{0,2}|[3-9][0-9]{0,1})" ;//正则表达式=。 =、懒得做文字处理了、
                Pattern p = Pattern. compile(reg);
                Matcher matcher = p.matcher(ip);
                if(matcher.find()){
                     ip = getDns();
                }
            } 
        } 
        //对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割 
        if(ip!=null && ip.length()>15){ //"***.***.***.***".length() = 15 
            if(ip.indexOf( ",")>0){ 
                ip = ip.substring(0,ip.indexOf( ",")); 
            } 
        } 
           
            return ip;
     }
     /**
      *
      * 功能说明:获取DNS服务器 地址
      * </p>
      * <p>参数说明: </p>
      * @return
      *
      */
    public   static   String   getDns()   { 
        String   s   =   ""; 
        try   { 
                String   s1   =   "ipconfig   /all"; 
                Process   process   =   Runtime.getRuntime().exec(s1); 
                BufferedReader   bufferedreader   =   new   BufferedReader( 
                                new   InputStreamReader(process.getInputStream())); 
                String   nextLine; 
                int num=0;
                for   (String   line   =   bufferedreader.readLine();   line   !=   null;   line   =   nextLine)   { 
                        nextLine   =   bufferedreader.readLine(); 
                        if   (line.indexOf( "DNS")   <=   0 )   { 
                                continue; 
                        }
                        num++;
                        if(num == 3){
                           s   =   line.substring(51-16);
                           System. out.println(s.length());
                           String regexString=".*(\\d{3}(\\.\\d{1,3}){3}).*" ;
                           s=s.replaceAll(regexString, "$1");
                            break; 
                        }
//                        int   i=0;
//                         i   =   line.indexOf("")   +   36; 
                       
                } 

                bufferedreader.close(); 
                process.waitFor(); 
        }   catch   (Exception   exception)   { 
                s   =   ""; 
        } 
        return   s.trim(); 
} 
}

 

转载于:https://my.oschina.net/u/2552286/blog/1572284

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值