⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ipmanager.java

📁 Email营销是在用户事先许可的前提下
💻 JAVA
字号:
package com.qq.content;

import javax.sql.rowset.CachedRowSet;

public class IPManager {
	
	private static  DBTool db;
	public IPManager() throws Exception{
		db=new DBTool();
	}

	
    private static long getLongFromIp(String ipStr) {
        long returnTotal = 0;

        ipStr = ipStr.replace('.', ':');
        String[] st = ipStr.split(":");

        if (st.length != 4)
            return returnTotal;
        String ipfield1 = st[0];
        String ipfield2 = st[1];
        String ipfield3 = st[2];
        String ipfield4 = st[3];

        long field1num = 0;
        long field2num = 0;
        long field3num = 0;
        long field4num = 0;
        try {
            field1num = Integer.parseInt(ipfield1);
            field2num = Integer.parseInt(ipfield2);
            field3num = Integer.parseInt(ipfield3);
            field4num = Integer.parseInt(ipfield4);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return returnTotal;
        }

        long returnField1 = field1num << 24;
        long returnField2 = field2num << 16;
        long returnField3 = field3num << 8;
      
        returnTotal = returnField1 + returnField2 + returnField3+field4num;
      
        return returnTotal;
    }
    
    public String[] getAddressByIP(String IP) throws Exception{
    	String [] address = new String[3];
    	for(int i=0;i<address.length;i++){
    		address[i] = "";
    	}
    	long longIP = getLongFromIp(IP);
    	String sql = "select country,province,city from city_ip_region_myisam where " + longIP + " between start_ip and end_ip";
    	CachedRowSet rs = db.query(sql);
    	if(rs.next()){
    		address[0] = rs.getString("country");
    		address[1] = rs.getString("province");
    		address[2] = rs.getString("city");
    	}
    	return address;
    }
    
    public String[] getAddressByLong(long longIP) throws Exception{
    	String [] address = new String[3];
    	for(int i=0;i<address.length;i++){
    		address[i] = "";
    	}
    	String sql = "SELECT country, province , city FROM city_ip_region_myisam WHERE " + longIP + " between start_ip and end_ip";
    	CachedRowSet rs = db.query(sql);
    	if(rs.next()){
    		address[0] = rs.getString("country");
    		address[1] = rs.getString("province");
    		address[2] = rs.getString("city");
    	}
    	return address;
    }
    
    
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try{
			System.out.println("begin");
		IPManager seeker = new IPManager();
		String [] adrs = seeker.getAddressByLong(16777218);
		System.out.println(adrs[0]+adrs[1]+adrs[2]);
		}catch(Exception e)
		{
			
			System.out.println("haha"+e.getMessage());
		}
		// TODO Auto-generated method stub

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -