IP地址的精细化操作(将IP地址转为相对应的地址)
第一步:
导入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;
} }
IP地址的精细化操作(将IP地址转为相对应的地址)的更多相关文章
- linux,windows下检测指定的IP地址是否可用或者检测IP地址冲突的3种方式(批处理程序,python程序,linux shell 批量ping)
本文中的脚本适用范围: 1)检测某些IP地址是否被占用: 2)检测网络中某些设备是否存活: 3)在分配新的ip地址之前,批量检测环境中是否存在冲突的机器 以上检测基于ICMP Ping报文,要求所有的 ...
- ip地址掩码和位数对应关系表、子网掩码、网络地址、主机地址-yellowcong
本文链接:https://blog.csdn.net/yelllowcong/article/details/76736594ip的地址掩码,刚开始感觉特别蒙蔽,网掩码都是每段8位二进制,共32位,子 ...
- java正则表达式小练习(IP地址检测、排序,叠词的处理,邮件地址的获取)
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; import java.util.reg ...
- IP地址分类及私网IP
5类IP地址: IP地址共有32位字节,其中A~C类IP地址由类标识号.网络地址和主机地址组成,A类标识最高位为0,网络地址为1字节,主机地址为3字节, B类标识最高位为10,网络地址为2字节,主机地 ...
- C#获取本机IP方法,获取本机局域网IP地址方法
1. private void GetIP() { string hostName = Dns.GetHostName();//本机名 //System.Net.IPAddress[] address ...
- MySQL远程(IP)连接报错:Host 'IP地址' is not allowed to connect to this MySQL server
ERROR 1130: Host ’192.168.1.3′ is not allowed to connect to this MySQL server这是告诉你没有权限连接指定IP的主机,下面我们 ...
- Nginx禁止直接通过IP地址访问网站以及限制IP登陆某目录(关闭默认站点或空主机头)
这篇文章主要介绍了Nginx中禁止使用IP访问网站的配置实例,一般在备案时可能需要这种设置,需要的朋友可以参考下 国内因为备案的原因,所有服务器都要禁止使用IP访问网站.否则,如果允许使用IP访问 ...
- 在计算机通信中,可靠交付应当由谁来负责?是网络还是端系统? 网络层协议 MAC帧、IP数据报、TCP报文 关系 IP地址与硬件地址 链路层与网络层
小结: 1. 网络层两种服务 虚电路服务 virtual circuit 电信网 网络层负责可靠交付 数据报服务 网络层不负责可靠交付 提供灵活的.无连接的.尽最大努力交付的数据报服务 不提供服务 ...
- PHP获取IP地址的方法,防止伪造IP地址注入攻击
PHP获取IP地址的方法 /** * 获取客户端IP地址 * <br />来源:ThinkPHP * <br />"X-FORWARDED-FOR" 是代理 ...
- IP地址及子网--四种IP广播地址
国际规定:把所有的IP地址划分为 A,B,C,D,E. 类默认子网掩码:A类为 255.0.0.0; B类为 255.255.0.0; C类为 255.255.255.0.子网掩码是一个32位地址,用 ...
随机推荐
- FFmpeg开发笔记(二十六)Linux环境安装ZLMediaKit实现视频推流
<FFmpeg开发实战:从零基础到短视频上线>一书在第10章介绍了轻量级流媒体服务器MediaMTX,通过该工具可以测试RTSP/RTMP等流媒体协议的推拉流.不过MediaMTX的功能 ...
- yum install 出错
``` yum -y install ansible ``` 出错 一直在刷屏,刚开始以为在安装但是最后没有安装成功 百度发现是这是redhat7和8的yum源混用 1.先确认本机器系统信息 cat ...
- C# asp.net mvc 创建虚拟目录
使用背景: 虚拟目录(virtual directory),计算机术语,每个 Internet服务可以从多个目录中发布.通过以通用命名约定 (UNC) 名.用户名及用于访问权限的密码指定目录,可将每个 ...
- 算法学习笔记(35): CMD Tree
对于 CMD Tree 的理解 原文:# 一种轻量级平衡树 这,EXSGT,感觉很像支持分裂 WBLT,但是相对来说思路很简单. 首先,在原文中说了: 能以均摊 \(\Theta(\log n)\) ...
- fontawesome-webfont.woff:1 Failed to load resource: the server responded with a status of 404 ()
fontawesome-webfont.woff2:1 Failed to load resource: the server responded with a status of 404 ()fon ...
- dubbo~全局异常拦截器的使用与设计缺陷~续
上一次的介绍,主要围绕如何统一去捕获异常,以及为每一种异常添加自己的Mapper实现,并且我们知道,当在ExceptionMapper中返回非200的Response,不支持application/j ...
- work02
第一题: 看程序说答案 int a = 10; int b = 3; int c = a + b;//13 int d = a - b;//7 int e = a * b; //30 int f = ...
- 安卓app 地铁最短路径查询 完成
我通过三个函数 完成了这个功能 首先 创建哈希表 根据起始站名 终点站名 然后 根据哈希表 建立起 邻接表' 最后 根据迪杰斯特拉算法 完成这个功能 /** * function:起终查询 */ / ...
- Thread的join方法demo
Thread的join方法demo /** * 关于join官方的解释是 Waits for this thread to die. 也就是等待一个线程结束. */ public class Thre ...
- 随机二次元图片API上线
Tips:当你看到这个提示的时候,说明当前的文章是由原emlog博客系统搬迁至此的,文章发布时间已过于久远,编排和内容不一定完整,还请谅解` 随机二次元图片API上线 日期:2017-12-6 阿珏 ...