零基础学习java------23---------动态代理,ip,url案例

1. 动态代理

  

 

 

2. ip,url案例

给定的access.log是电信运营商的用户上网数据,第一个字段是时间, 第二个字段是ip地址,第三个字段是访问的网站,其他字段可以忽略不计。

第一个字段是网段的起始IP地址,第二个字段是网段的结束IP地址,第三个字段是网段的起始IP地址对应的十进制,第四个字段是网段的结束IP地址对应的十进制,第五个字段代表洲,第六个代表国家,第七个代表省,第八个代表城市,第九个区域,第十个运营商。其他字段可以忽略不计。

要求

  通过计算access.log中的用户行为数据,统计出各个省份访问量(一次请求记作一次独立的访问量),并按照各个省份的访问量的从高到低进行排序

步骤分析:

  1.读取ip规则数据(ip.txt),将每条数据封装在bean里面,并将所有的bean数据封装在list中

  2.将ip装成长整型数据,以便判断特定ip是在哪个省份

  3.使用二分查找IpBean,得出某个ip所在的IpBean,进而得到所属的省份

  4.读取用户行为数据(access.log),以province为key,访问量为value存入map

具体代码:

IpBean

public class IpBean {
    private String startIp;
    private String endIp;
    private Long startLongIp;
    private Long endLongIp;
    private String state;
    private String country;
    private String province;
    private String city;
    private String area;
    private String isp;
    @Override
    public String toString() {
        return "IpBean [startIp=" + startIp + ", endIp=" + endIp + ", startLongIp=" + startLongIp + ", endLongIp="
                + endLongIp + ", state=" + state + ", country=" + country + ", province=" + province + ", city=" + city
                + ", area=" + area + ", isp=" + isp + "]";
    }
    public String getStartIp() {
        return startIp;
    }
    public void setStartIp(String startIp) {
        this.startIp = startIp;
    }
    public String getEndIp() {
        return endIp;
    }
    public void setEndIp(String endIp) {
        this.endIp = endIp;
    }
    public Long getStartLongIp() {
        return startLongIp;
    }
    public void setStartLongIp(Long startLongIp) {
        this.startLongIp = startLongIp;
    }
    public Long getEndLongIp() {
        return endLongIp;
    }
    public void setEndLongIp(Long endLongIp) {
        this.endLongIp = endLongIp;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    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 city) {
        this.city = city;
    }
    public String getArea() {
        return area;
    }
    public void setArea(String area) {
        this.area = area;
    }
    public String getIsp() {
        return isp;
    }
    public void setIsp(String isp) {
        this.isp = isp;
    }
    public IpBean() {
        
    }
    public void set(String startIp, String endIp, Long startLongIp, Long endLongIp, String state, String country,
            String province, String city, String area, String isp) {
        this.startIp = startIp;
        this.endIp = endIp;
        this.startLongIp = startLongIp;
        this.endLongIp = endLongIp;
        this.state = state;
        this.country = country;
        this.province = province;
        this.city = city;
        this.area = area;
        this.isp = isp;
    }
}
View Code

IpUtils

public class IpUtils {
    public static void main(String[] args) {
//        loadIpToList();
//        binaryOfIp(3738648971L);
    }
    // 将相应的字段存入IpBean,并将IpBean存入list
    public static List<IpBean> loadIpToList() {
        List<IpBean> list = new ArrayList<>();
        try (
                BufferedReader br = new BufferedReader(new FileReader("E:\\javafile\\ip.txt"));                
                ){
            String line = null;
            while((line=br.readLine()) != null) {
                 String[] split = line.split("\\|");
                try {
                    String startIp = split[0];
                    String endIp = split[1];
                    Long startLongIp;
                    Long endLongIp;
                    startLongIp = Long.parseLong(split[2]);
                    endLongIp = Long.parseLong(split[3]);
                    String state = split[4];
                    String country = split[5];
                    String province = split[6];
                    String city = split[7];
                    String area = split[8];
                    String isp = split[9]; 
                    IpBean ipBean = new IpBean();
                    ipBean.set(startIp, endIp, startLongIp, endLongIp, state, country, province, city, area, isp);
                    list.add(ipBean);
                } catch (Exception e) {
                    continue;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    
    // 将字符串ip转成长整型ip
    public static long parseStrIpToLongIp(String ip){
        long res = 0l; //192.168.133.34
        if(ip == null){
            return res ;
        }else{
            String[] ips = ip.split("\\.");
            if(ips.length == 4){
                for (int i = 0; i < ips.length; i++) {
                    long l = Long.parseLong(ips[i]);// 
                    res |= l<<((3-i)<<3) ;
                }
            }
        }
        return res; 
    }
    
    // 用来获取特定ip所属的ipBean
    public static IpBean binaryOfIp(Long ip, List<IpBean> list) {
        int start = 0;
        int end = list.size()-1;
        while(start <= end) {
            int middle = (start+end)/2;
            IpBean ipBean = list.get(middle);
            if(ip>=ipBean.getStartLongIp() && ip<=ipBean.getEndLongIp()) {
                return ipBean;
            }
            if(ip<ipBean.getStartLongIp()) {
                end = middle-1;
            }
            if(ip>ipBean.getEndLongIp()) {
                start = middle+1;
            }
        }
        return null;    
    }    
}
View Code

测试类(此代码错误)

public class IpTest {
    public static void main(String[] args) {
        Map<String, Integer> map = readData();
        System.out.println(map);
    }
    public static Map<String, Integer> readData() {
        Map<String, Integer> map = new HashMap<>();
        // 将数据的加载放入静态代码块
        List<IpBean> list;
        {
            list = IpUtils.loadIpToList();
        }
        // 获取以province为key,访问量为value的map
        try (
                BufferedReader br = new BufferedReader(new FileReader("E:\\javafile\\access.log"));
                ){
            String line = null;
            Integer sum = 1;
            while((line=br.readLine()) != null) {
                String[] split = line.split("\\|");
                String date = split[0];
                String ip = split[1];
                String url = split[2];
                Long ip1 = IpUtils.parseStrIpToLongIp(ip);
                
                IpBean bean = IpUtils.binaryOfIp(ip1, list);
                if(bean != null) {
                    if(map.containsKey(bean.getProvince())) {
                        sum+=1;
                        map.put(bean.getProvince(), sum);
                    }
                    map.put(bean.getProvince(), sum);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
View Code

 

posted @ 2019-09-08 11:05  一y样  阅读(322)  评论(0编辑  收藏  举报