首页 > 其他分享 >IP地址的精细化操作(将IP地址转为相对应的地址)

IP地址的精细化操作(将IP地址转为相对应的地址)

时间:2022-10-20 20:34:10浏览次数:60  
标签:精细化 java String new 地址 io IP地址 import

第一步:

导入maven依赖:

  <dependency>
                <groupId>org.lionsoul</groupId>
                <artifactId>ip2region</artifactId>
                <version>1.7.2</version>
            </dependency>

第二步,下载ip2region.db并将其放在resours下

 

第三步

插入代码

ceshiIP.java

package test;

import java.io.IOException;
import java.net.InetAddress;
import test.ttt;

public class ceshiIP0 {

public static void main(String[] args) throws IOException {
//获取IP地址
ttt t = new ttt();

String [] p=t.arr();//调用ttt.下的arr方法
String [] q=new String[p.length];//将获取后的城市加入数组中
for (int i=0;i<p.length;i++){
String hostAddress = p[i];
String detail = data1.getCityInfo(hostAddress);
q[i]="IP地址:"+hostAddress+"地区:"+detail;
System.out.println(q[i]);
// System.out.println("IP地址:"+hostAddress+"地区:"+detail);
}
}
}

data1.java(处理ip地址转化为城市)

package test;

import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;
import org.lionsoul.ip2region.Util;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;

public class data1 {

    /**
     * 根据IP地址获取城市
     * @param ip
     * 如果放在服务器读取不了,两种方式
     * 1.可以在服务器上创建文件,ip2region.db这个放在文件里面,然后开始读取
     * 2.可以整个配置文件,在配置配置目录,然后读取配置文件
     * @return
     */
    public static String getCityInfo(String ip) throws IOException {
        URL url = data1.class.getClassLoader().getResource("ip2region.db");
        File file;
        if (url != null) {
            file = new File(url.getFile());
        } else {
            return null;
        }
        if (!file.exists()) {
            System.out.println("Error: Invalid ip2region.db file, filePath:" + file.getPath());
            return null;
        }
        //查询算法
        int algorithm = DbSearcher.BTREE_ALGORITHM; //B-tree
        //DbSearcher.BINARY_ALGORITHM //Binary
        //DbSearcher.MEMORY_ALGORITYM //Memory
        try {
            DbConfig config = new DbConfig();
            DbSearcher searcher = new DbSearcher(config, file.getPath());
            Method method;
            switch ( algorithm )
            {
                case DbSearcher.BTREE_ALGORITHM:
                    method = searcher.getClass().getMethod("btreeSearch", String.class);
                    break;
                case DbSearcher.BINARY_ALGORITHM:
                    method = searcher.getClass().getMethod("binarySearch", String.class);
                    break;
                case DbSearcher.MEMORY_ALGORITYM:
                    method = searcher.getClass().getMethod("memorySearch", String.class);
                    break;
                default:
                    return null;
            }
            DataBlock dataBlock;
            if (!Util.isIpAddress(ip)) {
                System.out.println("Error: Invalid ip address");
                return null;
            }
            dataBlock  = (DataBlock) method.invoke(searcher, ip);
            return dataBlock.getRegion();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}

ttt.java(从文件中取IP地址)

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class ttt {
    public static void main(String[] args) throws IOException {
        String []p=new String[2000];
        p=arr();
        for (int i=0;i<p.length;i++){
            System.out.println(p[i]);}
    }
    public static String [] arr() throws IOException {
        String fileName = "src/main/java/result.txt";
        File file = new File(fileName);
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String line;
        int index=0;
        String[] arr=new String[2000];
        while((line = br.readLine()) != null){
            //process the line
//            System.out.println(line);
            arr[index]=line;
            index++;
        }
        String [] b=new String[index];
        String [] a=new String[index];
        for(int i=0;i<index;i++)
        {
//            System.out.println("*********88"+a[0]);

            a=arr[i].split(",");
//                System.out.println(a[0]);
            b[i]=a[0];

        }
        return b;
    }
}

 

Data.java(清洗时间)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

public class Data {

    public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
        FileReader read = new FileReader("src/main/java/result.txt");//要清洗的文件
        BufferedReader br = new BufferedReader(read);
        Writer writer = null;
        File outFile = new File("result2.txt");//清洗后的文件存放位置
        writer = new OutputStreamWriter(new FileOutputStream(outFile),"utf-8");
        BufferedWriter bw = new BufferedWriter(writer);
        String row;
        String[] data=new String[6];
        int hang=1;
        try {
            while((row = br.readLine())!=null){
                data=change(row);
                data=chage(data);
                for(int i=0;i<data.length;i++) {
                    System.out.print(data[i]+"\t");
                }
                System.out.println();
                row=data[0]+","+data[1]+","+data[2]+","+data[3]+","+data[4]+","+data[5];
                bw.write(row + "\r\n");
                //i++;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private static String[] chage(String[] data) {
        /*
         * for(int i=0;i<data.length;i++) { data[] }
         */
        data[0]=data[0];
        char[] str=data[1].toCharArray();
        String[] time=new String[7];
        int j=0;
        int k=0;
        for(int i=0;i<str.length;i++) {
            if(str[i]=='/'||str[i]==':'||str[i]==32) {
                time[k]=data[1].substring(j,i);
                j=i+1;
                k++;
            }
        }
        time[k]=data[1].substring(j, data[1].length());
        switch(time[1]) { case "Jan":time[1]="01";break; case
                "Feb":time[1]="02";break; case "Mar":time[1]="03";break; case
                "Apr":time[1]="04";break; case "May":time[1]="05";break; case
                "Jun":time[1]="06";break; case "Jul":time[1]="07";break; case
                "Aug":time[1]="08";break; case "Sep":time[1]="09";break; case
                "Oct":time[1]="10";break; case "Nov":time[1]="11";break; case
                "Dec":time[1]="12";break; }

        data[1]=time[2]+"-"+time[1]+"-"+time[0]+" "+time[3]+":"+time[4]+":"+time[5];
        data[3]=data[3].substring(0, data[3].length()-1);
        return data;
    }

    private static String [] change(String row) {
        char [] str1=row.toCharArray();
        String [] data =new String [6];
        int j=0;
        int k=0;
        for(int i=0;i<str1.length;i++) {
            if(str1[i]==',') {
                data[k]=row.substring(j, i);
                j=i+1;
                k++;
            }
        }
        data[k]=row.substring(j, str1.length);
        return data;
    }

}

 

标签:精细化,java,String,new,地址,io,IP地址,import
From: https://www.cnblogs.com/jyt604743080/p/16811178.html

相关文章