java根据ip定位地理位置

在项目开发中,我们可能会遇到这样的需求:需要在登录日志或者操作日志中记录客户端ip所在的地理位置。目前根据ip定位地理位置的第三方api有好几个,淘宝、新浪、百度等,这三种其实也有些缺点的:淘宝,开始几次可以成功根据ip获取对应地理位置,但后面就莫名其妙开始不行,直接通过浏览器获取又可以;新浪,之前一直可以,但最近不知道为什么不行了,访问直接报错(可能api修改了或者取消了吧);百度,需要申请百度地图开发者平台的开发者账号,请求时接口中需要加上百度提供的秘钥等信息,我试了下,好像不能定位国外的ip。

IPLocation.java

/**
 * 用来封装ip相关信息,目前只有两个字段,ip所在的国家和地区
 *
 */
public class IPLocation {

	private String country;  
    private String area;  
      
    public IPLocation() {  
        country = area = "";  
    }  
      
    public IPLocation getCopy() {  
        IPLocation ret = new IPLocation();  
        ret.country = country;  
        ret.area = area;  
        return ret;  
    }  
  
    public String getCountry() {  
        return country;  
    }  
  
    public void setCountry(String country) {  
        this.country = country;  
    }  
  
    public String getArea() {  
        return area;  
    }  
  
    public void setArea(String area) {  
        //如果为局域网,纯真IP地址库的地区会显示CZ88.NET,这里把它去掉  
        if(area.trim().equals("CZ88.NET")){  
            this.area="本机或本网络";  
        }else{  
            this.area = area;  
        }  
    }
	
}

Message.java

public class Message {

	public static String bad_ip_file="IP地址库文件错误";  
	public static String unknown_country="未知国家";  
	public static String unknown_area="未知地区";
	
}

IPSeeker.java

PropertiesUtil.java

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** 
 * 读取properties文件
 */
public class PropertiesUtil {

	private static final Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
	
	private static Map<String, String> map = null;
	
	public static String get(String key){
		if(map == null) {
			load();
		}
		return map.get(key);
	}
	
	public static Integer getInt(String key){
		if(map == null) {
			load();
		}
		String value = map.get(key);
		return Integer.parseInt(value);
	}
	
	synchronized static private void load(){
		logger.info("开始加载properties文件内容......");
		map = new HashMap<String, String>();
		Properties props = null;
		InputStream in = null;
		try {
			props = new Properties();
			in = PropertiesUtil.class.getClassLoader().getResourceAsStream("project.properties");
            //防止properties中的中文读取出来后乱码
			props.load(new InputStreamReader(in, "utf-8"));
			Set<Entry<Object, Object>> entries = props.entrySet();
			Iterator<Entry<Object, Object>> it =  entries.iterator();
			Entry<Object, Object> entry = null;
			String key = null;
			String value = null;
			while(it.hasNext()){
				entry = it.next();
				key = (String)entry.getKey();
				value = (String)entry.getValue();
				map.put(key, value);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("读取properties文件出现异常......");
		} finally {
			try {
				if(null != in) {
                    in.close();
                }
			} catch (Exception e2) {
				e2.printStackTrace();
				logger.error("properties文件流关闭出现异常......");
			}
		}
		logger.info("properties文件内容加载完成......");
	}
	
}
经验分享 程序员 微信小程序 职场和发展