当前位置:网站首页>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);
边栏推荐
- 高等數學---第八章多元函數微分學1
- postgresql array类型,每一项拼接
- Solve the cache breakdown problem
- Flask session forged hctf admin
- 手里的闲钱是炒股票还是买理财产品好?
- 请问,如图,pyhon云函数提示使用了 pymysql模块,这个是怎么回事?
- 2022-7-6 Leetcode27.移除元素——太久没有做题了,为双指针如此狼狈的一天
- 2022-7-6 初学redis(一)在 Linux 下下载安装并运行 redis
- 属性关键字Aliases,Calculated,Cardinality,ClientName
- 3D Detection: 3D Box和点云 快速可视化
猜你喜欢
Indoor ROS robot navigation commissioning record (experience in selecting expansion radius)
Xshell connection server changes key login to password login
118. Yanghui triangle
Vmware共享主机的有线网络IP地址
2022-7-7 Leetcode 844. Compare strings with backspace
高等数学---第八章多元函数微分学1
Social responsibility · value co creation, Zhongguancun network security and Information Industry Alliance dialogue, wechat entrepreneur Haitai Fangyuan, chairman Mr. Jiang Haizhou
Mathématiques avancées - - chapitre 8 différenciation des fonctions multivariables 1
2022-7-6 Leetcode 977. Square of ordered array
Leecode3. Longest substring without repeated characters
随机推荐
Realization of search box effect [daily question]
AI talent cultivation new ideas, this live broadcast has what you care about
move base参数解析及经验总结
Navicat运行sql文件导入数据不全或导入失败
Regular expression integer positive integer some basic expressions
Vmware 与主机之间传输文件
Environment configuration
Is it safe to open an account online now? Which securities company should I choose to open an account online?
搜索框效果的实现【每日一题】
Lavarel之环境配置 .env
供应链供需预估-[时间序列]
Flink | multi stream conversion
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
[untitled]
Wired network IP address of VMware shared host
《厌女:日本的女性嫌恶》摘录
请问,我kafka 3个分区,flinksql 任务中 写了 join操作,,我怎么单独给join
Data refresh of recyclerview
Vmware共享主机的有线网络IP地址
Interface automation test - solution of data dependency between interfaces