当前位置:网站首页>IP address home location query
IP address home location query
2022-07-07 14:04:00 【It is small new】
The goal is
By developing IP Address attribution query platform , We need to be right about JavaSE The comprehensive technology has been improved , Enhance actual combat capability . After studying this project, we should have the following abilities :
1 Object oriented programming
2 Tool class encapsulation and usage writing
3 file IO flow
4 string manipulation
5 Binary search
6 IP Different forms of address use
Ideas
1 Read the content in the program
2 analysis IP character string , Structured processing
3 Wrapper utility class
4 Interface API
Enter the reference : IP
The ginseng : Place of ownership
Code development
Read the file
public static List<String>getLineList(String filePath,String encoding)throws IOException{
// Node stream docking file
FileInputStream fis=new FileInputStream(filePath);
// Convert to character And specify character encoding
Reader reader=new InputStreamReader(fis,encoding);
// Buffering streams improves efficiency
BufferedReader br=new BufferedReader(reader);
// Read
String line=null;
// Save the read data
List<String>lineList=new ArrayList<String>();
while((line=br.readLine())!=null){
// Add to collection
lineList.add(line);
}
// close
br.close();
return lineList;
structured ip Address entity class
*
*
*/
public class IPAndLocationPojo implements Comparable<IPAndLocationPojo>{
// Derived fields Used to hold ip Corresponding long value
private long startIPLong;
private long endIPLong;
// start IP
private String startIP;
// end IP
private String endIP;
// Place of ownership
private String location;
public int compareTo(IPAndLocationPojo o){
long status=this.startIPLong-o.startIPLong;
// Cannot cast If the two values differ 2127483647 Words Convert to int after Get a negative number
//return(int)(this.start.IPLong-o.startIPLong);
return status>0?1:0;
}
public IPAndLocationPojo(long startIPLong, long endIPLong, String startIP,
String endIP, String location) {
super();
// Assign a value to a long integer
this.startIPLong = IPUtil.ipToLong(startIP);;
this.endIPLong = IPUtil.ipToLong(endIP);;
this.startIP = startIP;
this.endIP = endIP;
this.location = location;
}
public long getStartIPLong() {
return startIPLong;
}
public void setStartIPLong(long startIPLong) {
this.startIPLong = startIPLong;
}
public long getEndIPLong() {
return endIPLong;
}
public void setEndIPLong(long endIPLong) {
this.endIPLong = endIPLong;
}
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 String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public IPAndLocationPojo() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "IPAndLocationPojo [startIPLong=" + startIPLong + ", endIPLong="
+ endIPLong + ", startIP=" + startIP + ", endIP=" + endIP
+ ", location=" + location + "]";
}
Program core business class
*
*
*/
public class DataProcessManager {
private static IPAndLocationPojo[]ipAndLocationPojoArray=null;
static{
// File path
String ipLibrayPath="ip_location_relation.txt";
String encoding="UTF-8";
// Save data object
List<IPAndLocationPojo>ipAndLocationPojos=null;
try {
// get data
ipAndLocationPojos = DataProcessManager.getPojoList(ipLibrayPath,
encoding);
// Turn the array and sort
ipAndLocationPojoArray = DataProcessManager
.convertListToArraySort(ipAndLocationPojos);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* External interface Participation is ip The exit is the place of belonging
* @param ipAndLocationPojos
* @return
*/
public static String getLocation(String ip){
// Binary search
int index=DataProcessManager.binaraySeach(ipAndLocationPojoArray, ip);
// Determine if it is found
if (index == -1) {
return null;
} else {
return ipAndLocationPojoArray[index].getLocation();
}
}
/**
* Binary search , Participation is IP And an array , The output parameter is the corresponding index , No return found -1;
* @param ipAndLocationPojos
* @return
*/
public static int binaraySeach(IPAndLocationPojo[] ipAndLocationPojoArray,
String targetIP) {
// hold IP Convert to long
long targetIPLong = IPUtil.ipToLong(targetIP);
int startIndex = 0;
int endIndex = ipAndLocationPojoArray.length - 1;
int m = (startIndex + endIndex) / 2;
/**
* If Less than start IP Find the front
*
* If Greater than start IP Find the back
*
* If Greater than or equal to start IP And Less than or equal to end IP It says it found it
*/
while (startIndex <= endIndex) {
if (targetIPLong >= ipAndLocationPojoArray[m].getStartIPLong()
&& targetIPLong <= ipAndLocationPojoArray[m].getEndIPLong()) {
return m;
}
if (targetIPLong < ipAndLocationPojoArray[m].getStartIPLong()) {
endIndex = m - 1;
} else {
startIndex = m + 1;
}
m = (startIndex + endIndex) / 2;
}
return -1;
}
// Convert the set into an array and sort
public static IPAndLocationPojo[]convertListArraySort(List<IPAndLocationPojo>ipAndLocationPojos){
// Create array
IPAndLocationPojo[]ipAndLocationPojoArray=new IPAndLocationPojo[ipAndLocationPojos.size()];
// Convert to array
ipAndLocationPojos.toArray(ipAndLocationPojoArray);
// Sort
Arrays.sort(ipAndLocationPojoArray);
return ipAndLocationPojoArray;
}
// Structured data collection
public static List<IPAndLocationPojo>getPojoList(String filePath,String encoding)throws IOException{
// Save data object
List<IPAndLocationPojo>ipAndLocationPojos=new ArrayList<IPAndLocationPojo>();
List<String>lineList=FileOperatorUtil.getLineList(filePath, encoding);
for(String string:lineList){
// Judge whether it is an empty line
if(string==null||string.trim().equals("")){
continue;
}
// Split array
String[] columnArray=string.split(" ");
// Get start ip
String startIP=columnArray[0];
// End of acquisition ip
String endIP=columnArray[1];
// Get the place of belonging
String location=columnArray[2];
// Encapsulate into objects
IPAndLocationPojo ipAndLocationPojo=new IPAndLocationPojo(startIP,endIP,location);
// Add to collection
ipAndLocationPojos.add(ipAndLocationPojo);
}
return ipAndLocationPojos;// entrance
public class SystemController {
@SuppressWarnings("resource")
public static void main(String[] args) {
// Receive user input
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println(" Please enter IP Address : ");
String ip = scanner.nextLine();
// Inquire about
long startTime = System.currentTimeMillis();
String location = DataProcessManager.getLocation(ip);
long endTime = System.currentTimeMillis();
System.out.println(" Time consuming : " + (endTime - startTime) + " "
+ location);边栏推荐
- Getting started with MySQL
- Transferring files between VMware and host
- FCOS3D label assignment
- Laravel Form-builder使用
- Leetcode simple question sharing (20)
- C语言数组相关问题深度理解
- Excusez - moi, l'exécution a été réussie lors de l'utilisation des données de puits SQL Flink à Kafka, mais il n'y a pas de nombre dans Kafka
- Best practice | using Tencent cloud AI willingness to audit as the escort of telephone compliance
- PostgreSQL array type, each splice
- Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
猜你喜欢

Help tenants

. Net core about redis pipeline and transactions

Navicat run SQL file import data incomplete or import failed

高等数学---第八章多元函数微分学1

Wired network IP address of VMware shared host

实现IP地址归属地显示功能、号码归属地查询

MySQL error 28 and solution

干货|总结那些漏洞工具的联动使用

.net core 关于redis的pipeline以及事务

Redis 核心数据结构 & Redis 6 新特性详
随机推荐
AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
【堡垒机】云堡垒机和普通堡垒机的区别是什么?
【网络安全】sql注入语法汇总
现在网上开户安全么?那么网上开户选哪个证券公司?
2022-7-6 beginner redis (I) download, install and run redis under Linux
Help tenants
Take you to master the three-tier architecture (recommended Collection)
Flask session forged hctf admin
云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
mysql导入文件出现Data truncated for column ‘xxx’ at row 1的原因
How to check the ram and ROM usage of MCU through Keil
2022-7-6 Leetcode 977. Square of ordered array
js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点
华为镜像地址
118. 杨辉三角
Use of polarscatter function in MATLAB
flask session伪造之hctf admin
SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1
Dry goods | summarize the linkage use of those vulnerability tools
Toraw and markraw