当前位置:网站首页>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);
边栏推荐
- Laravel5 call to undefined function openssl cipher iv length() 报错 PHP7开启OpenSSL扩展失败
- Common response status codes
- 交付效率提升52倍,运营效率提升10倍,看《金融云原生技术实践案例汇编》(附下载)
- Indoor ROS robot navigation commissioning record (experience in selecting expansion radius)
- Did login metamask
- js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点
- AI人才培育新思路,这场直播有你关心的
- 现在网上开户安全么?那么网上开户选哪个证券公司?
- 2022-7-6 使用SIGURG来接受外带数据,不知道为什么打印不出来
- 3D detection: fast visualization of 3D box and point cloud
猜你喜欢
【堡垒机】云堡垒机和普通堡垒机的区别是什么?
LeetCode简单题分享(20)
Supply chain supply and demand estimation - [time series]
2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
Leecode3. Longest substring without repeated characters
2022-7-6 beginner redis (I) download, install and run redis under Linux
Xshell connection server changes key login to password login
Vmware共享主机的有线网络IP地址
Battle Atlas: 12 scenarios detailing the requirements for container safety construction
Custom thread pool rejection policy
随机推荐
请问,redis没有消费消息,都在redis里堆着是怎么回事?用的是cerely 。
MySQL "invalid use of null value" solution
Mysql怎样控制replace替换的次数?
[untitled]
2022-7-6 Leetcode27. Remove the element - I haven't done the problem for a long time. It's such an embarrassing day for double pointers
118. Yanghui triangle
2022-7-6 使用SIGURG来接受外带数据,不知道为什么打印不出来
室内ROS机器人导航调试记录(膨胀半径的选取经验)
mysql ”Invalid use of null value“ 解决方法
Wired network IP address of VMware shared host
使用day.js让时间 (显示为几分钟前 几小时前 几天前 几个月前 )
FCOS3D label assignment
Help tenants
2022-7-7 Leetcode 34. Find the first and last positions of elements in a sorted array
作战图鉴:12大场景详述容器安全建设要求
move base参数解析及经验总结
docker部署oracle
2022-7-6 初学redis(一)在 Linux 下下载安装并运行 redis
566. Reshaping the matrix
Redis can only cache? Too out!