省份浏览量统计之IP库解析

IPParser.java

package com.imooc.bigdata.hadoop.hdfs.mr.project.utils;

public class IPParser extends IPSeeker {
    // 地址 仅仅只是在ecplise环境中使用,部署在服务器上,需要先将qqwry.dat放在集群的各个节点某个有读取权限目录,
    // 然后在这里指定全路径
    private static final String ipFilePath = "ip/qqwry.dat";
    // 部署在服务器上
    //private static final String ipFilePath = "/opt/datas/qqwry.dat";
    private static IPParser obj = new IPParser(ipFilePath);



    protected IPParser(String ipFilePath) {
        super(ipFilePath);
    }

    public static IPParser getInstance() {
        return obj;
    }

    /**
     * 解析ip地址
     *
     * @param ip
     * @return
     */
    public RegionInfo analyseIp(String ip) {
        if (ip == null || "".equals(ip.trim())) {
            return null;
        }

        RegionInfo info = new RegionInfo();
        try {
            String country = super.getCountry(ip);
            if ("局域网".equals(country) || country == null || country.isEmpty() || country.trim().startsWith("CZ88")) {
                // 设置默认值
                info.setCountry("中国");
                info.setProvince("上海市");
            } else {
                int length = country.length();
                int index = country.indexOf('省');
                if (index > 0) { // 表示是国内的某个省
                    info.setCountry("中国");
                    info.setProvince(country.substring(0, Math.min(index + 1, length)));
                    int index2 = country.indexOf('市', index);
                    if (index2 > 0) {
                        // 设置市
                        info.setCity(country.substring(index + 1, Math.min(index2 + 1, length)));
                    }
                } else {
                    String flag = country.substring(0, 2);
                    switch (flag) {
                        case "内蒙":
                            info.setCountry("中国");
                            info.setProvince("内蒙古自治区");
                            country = country.substring(3);
                            if (country != null && !country.isEmpty()) {
                                index = country.indexOf('市');
                                if (index > 0) {
                                    // 设置市
                                    info.setCity(country.substring(0, Math.min(index + 1, length)));
                                }
                                // TODO:针对其他旗或者盟没有进行处理
                            }
                            break;
                        case "广西":
                        case "西藏":
                        case "宁夏":
                        case "新疆":
                            info.setCountry("中国");
                            info.setProvince(flag);
                            country = country.substring(2);
                            if (country != null && !country.isEmpty()) {
                                index = country.indexOf('市');
                                if (index > 0) {
                                    // 设置市
                                    info.setCity(country.substring(0, Math.min(index + 1, length)));
                                }
                            }
                            break;
                        case "上海":
                        case "北京":
                        case "重庆":
                        case "天津":
                            info.setCountry("中国");
                            info.setProvince(flag + "市");
                            country = country.substring(3);
                            if (country != null && !country.isEmpty()) {
                                index = country.indexOf('区');
                                if (index > 0) {
                                    // 设置市
                                    char ch = country.charAt(index - 1);
                                    if (ch != '小' || ch != '校') {
                                        info.setCity(country.substring(0, Math.min(index + 1, length)));
                                    }
                                }

                                if ("unknown".equals(info.getCity())) {
                                    // 现在city还没有设置,考虑县
                                    index = country.indexOf('县');
                                    if (index > 0) {
                                        // 设置市
                                        info.setCity(country.substring(0, Math.min(index + 1, length)));
                                    }
                                }
                            }
                            break;
                        case "香港":
                        case "澳门":
                            info.setCountry("中国");
                            info.setProvince(flag + "特别行政区");
                            break;
                        default:
                            info.setCountry(country); // 针对其他国外的ip
                    }
                }
            }
        } catch (Exception e) {
            // nothing
        }
        return info;
    }

    /**
     * ip地址对应的info类
     *
     */
    public static class RegionInfo {
        private String country ;
        private String province ;
        private String city ;

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        public String getProvince() {
            return province;
        }

        public void setProvince(String province) {
            this.province = province;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String c
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
全国IP地址,十分详细 范例。 province city isp start_ip1 end_ip1 福建省 电信 1.0.1.0 1.0.3.255 广东省 电信 1.0.8.0 1.0.15.255 广东省 电信 1.0.32.0 1.0.63.255 福建省 电信 1.1.0.0 1.1.0.255 福建省 电信 1.1.2.0 1.1.3.255 福建省 电信 1.1.4.0 1.1.7.255 广东省 电信 1.1.8.0 1.1.63.255 福建省 电信 1.2.0.0 1.2.1.255 北京市 (北龙中网科技有限公司) 网通 1.2.2.0 1.2.2.255 福建省 电信 1.2.4.0 1.2.7.255 广东省 电信 1.2.8.0 1.2.127.255 广东省 电信 1.3.0.0 1.3.255.255 福建省 电信 1.4.1.0 1.4.3.255 福建省 电信 1.4.5.0 1.4.7.255 广东省 电信 1.4.8.0 1.4.127.255 广东省 电信 1.10.0.0 1.10.7.255 福建省 电信 1.10.8.0 1.10.9.255 福建省 电信 1.10.11.0 1.10.15.255 广东省 电信 1.10.16.0 1.10.127.255 北京市 方正宽带 1.12.0.0 1.15.255.255 内蒙古 联通 1.24.0.0 1.31.255.255 内蒙古 赤峰市 联通 1.24.8.0 1.24.15.255 内蒙古 乌兰察布市 联通 1.24.32.0 1.24.39.255 内蒙古 乌兰察布市 联通 1.24.80.0 1.24.87.255 内蒙古 包头市 联通 1.24.184.0 1.24.191.255 内蒙古 乌兰察布市 联通 1.25.128.0 1.25.135.255 内蒙古 锡林郭勒盟 联通 1.25.152.0 1.25.191.255 内蒙古 巴彦淖尔市 联通 1.25.228.0 1.25.255.255 内蒙古 呼伦贝尔市 联通 1.26.0.0 1.26.15.255 内蒙古 赤峰市 联通 1.26.112.0 1.26.127.255 贵州省 电信 1.48.0.0 1.49.255.255 贵州省 贵阳市(电信CDMA全省共用出口) 电信 1.49.128.0 1.49.255.255 宁夏 电信 1.50.0.0 1.50.255.255 山东省 济南市(山东大学) 教育网 1.51.192.0 1.51.255.255 黑龙江省 联通 1.56.0.0 1.63.255.255 黑龙江省 鹤岗市 联通 1.56.0.0 1.56.95.255 黑龙江省 牡丹江市 联通 1.56.128.0 1.56.255.255 黑龙江省 绥化市 联通 1.57.0.0 1.57.63.255 黑龙江省 齐齐哈尔市 联通 1.57.64.0 1.57.127.255 黑龙江省 双鸭山市 联通 1.57.128.0 1.57.191.255 黑龙江省 鸡西市 联通 1.57.192.0 1.57.255.255 黑龙江省 哈尔滨市 联通 1.58.0.0 1.58.255.255 黑龙江省 大庆市 联通 1.59.16.0 1.59.127.255 黑龙江省 佳木斯市 联通 1.59.128.0 1.59.255.255 黑龙江省 黑河市 联通 1.60.0.0 1.60.63.255 黑龙江省 七台河市 联通 1.60.64.0 1.60.95.255 黑龙江省 伊春市 联通 1.60.128.0 1.60.191.255 黑龙江省 齐齐哈尔市 联通 1.60.192.0 1.60.255.255 黑龙江省 绥化市 联通 1.61.0.0 1.61.127.255 黑龙江省 齐齐哈尔市 联通 1.61.128.0 1.61.159.255 黑龙江省 哈尔滨市 联通 1.62.0.0 1.62.127.255 黑龙江省 鸡西市 联通 1.63.0.0 1.63.31.255 黑龙江省 绥化市 联通 1.63.152.0 1.63.159.255 黑龙江省 伊春市 联通 1.63.192.0 1.63.207.255 山西省 电信 1.68.0.0 1.71.255.255 陕西省 电信 1.80.0.0 1.87.255.255 陕西省 西安市 电信 1.80.0.0 1.80.255.255 陕西省 渭南市 电信 1.81.0.0 1.81.127.255 陕西省 汉中市 电信 1.81.128.0 1.81.255.255 陕西省 商洛市 电信 1.82.0.0 1.82.31.255 陕西省 安康市 电信 1.82.32.0 1.82.63.255 陕西省 榆林市 电信 1.82.64.0 1.82.127.255 陕西省 宝鸡市 电信 1.82.128.0 1.82.163.255 陕西省 西安市 电信 1.83.0.0 1.83.255.255 陕西省 西安市 电信 1.84.64.0 1.84.127.255 陕西省 西安市 电信 1.85.0.0 1.85.23.255 陕西省 延安市 电信 1.85.64.0 1.85.95.255 陕西省 安康市 电信 1.85.96.0 1.85.135.255 陕西省 咸阳市 电信 1.85.144.0 1.85.159.255 陕西省 西安市 电信 1.85.172.0 1.85.191.255 陕西省 西安市 电信 1.85.192.0 1.86.255.255 陕西省 西安市 电信 1.87.0.0 1.87.255.255 北京市 歌华有线宽带 1.88.0.0 1.91.255.255 北京市 电信通 1.92.0.0 1.93.255.255 内蒙古 电信 1.180.0.0 1.183.255.255 内蒙古 鄂尔多斯市 电信 1.180.64.0 1.180.67.255 内蒙古 乌海市 电信 1.180.128.0 1.180.135.255 内蒙古 呼和浩特市 电信 1.182.0.0 1.182.3.255 广东省 广州市(暨南大学) 教育网 1.184.0.0 1.184.127.255 黑龙江省 联通 1.188.0.0 1.191.255.255 河南省 郑州市 电信 1.192.0.0 1.192.191.255 河南省 电信 1.192.0.0 1.199.255.255 河南省 郑州市 电信 1.193.0.0 1.193.127.255 河南省 洛阳市 电信 1.193.128.0 1.193.239.255 河南省 平顶山市 电信 1.194.0.0 1.194.63.255 河南省 开封市 电信 1.194.128.0 1.194.159.255 河南省 安阳市 电信 1.194.192.0 1.194.255.255 河南省 新乡市 电信 1.195.0.0 1.195.63.255 河南省 焦作市 电信 1.195.64.0 1.195.127.255 河南省 濮阳市 电信 1.195.128.0 1.195.147.255 河南省 三门峡市 电信 1.195.192.0 1.195.255.255 河南省 商丘市 电信 1.196.64.0 1.196.79.255 河南省 信阳市 电信 1.196.192.0 1.196.223.255 河南省 鹤壁市 电信 1.197.0.0 1.197.15.255 河南省 漯河市 电信 1.197.32.0 1.197.63.255 河南省 周口市 电信 1.197.64.0 1.197.95.255 河南省 驻马店市 电信 1.197.96.0 1.197.127.255 河南省 许昌市 电信 1.197.128.0 1.197.159.255 河南省 南阳市 电信 1.197.160.0 1.197.175.255 河南省 焦作市 电信 1.197.192.0 1.197.207.255 河南省 安阳市 电信 1.197.208.0 1.197.223.255 河南省 周口市 电信 1.197.224.0 1.197.235.255 河南省 周口市 电信 1.199.0.0 1.199.15.255 河南省 新乡市 电信 1.199.96.0 1.199.127.255 北京市 (电信WIFI热点AP网段) 电信 1.202.0.0 1.203.255.255 贵州省 贵阳市 电信 1.204.0.0 1.204.255.255
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值