当前位置:网站首页>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);边栏推荐
- move base参数解析及经验总结
- requires php ~7.1 -&gt; your PHP version (7.0.18) does not satisfy that requirement
- 搜索框效果的实现【每日一题】
- Attribute keywords aliases, calculated, cardinality, ClientName
- 566. 重塑矩阵
- js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点
- 3D Detection: 3D Box和点云 快速可视化
- Redis can only cache? Too out!
- Move base parameter analysis and experience summary
- Navicat run SQL file import data incomplete or import failed
猜你喜欢

. Net core about redis pipeline and transactions

Dry goods | summarize the linkage use of those vulnerability tools
![SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1](/img/10/6de1ee8467b18ae03894a8d5ba95ff.png)
SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1

Thread pool reject policy best practices

《厌女:日本的女性嫌恶》摘录

Vmware共享主机的有线网络IP地址

最长上升子序列模型 AcWing 1014. 登山

Build a secure and trusted computing platform based on Kunpeng's native security

2022-7-6 sigurg is used to receive external data. I don't know why it can't be printed out

2022-7-6 beginner redis (I) download, install and run redis under Linux
随机推荐
Common response status codes
使用day.js让时间 (显示为几分钟前 几小时前 几天前 几个月前 )
請問,在使用flink sql sink數據到kafka的時候出現執行成功,但是kafka裏面沒有數
Is the compass stock software reliable? Is it safe to trade stocks?
XML文件的解析操作
2022-7-6 Leetcode27.移除元素——太久没有做题了,为双指针如此狼狈的一天
《厌女:日本的女性嫌恶》摘录
Parameter keywords final, flags, internal, mapping keywords internal
请问,redis没有消费消息,都在redis里堆着是怎么回事?用的是cerely 。
Take you to master the three-tier architecture (recommended Collection)
搜索框效果的实现【每日一题】
mysql导入文件出现Data truncated for column ‘xxx’ at row 1的原因
PHP中用下划线开头的变量含义
请问,我kafka 3个分区,flinksql 任务中 写了 join操作,,我怎么单独给join
作战图鉴:12大场景详述容器安全建设要求
LeetCode简单题分享(20)
2022-7-7 Leetcode 844. Compare strings with backspace
接口自动化测试-接口间数据依赖问题解决
云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
flask session伪造之hctf admin