IP地址归属地查询
⽬标
通过开发IP地址归属地查询平台,我们需要对JavaSE综合技术有所提升,增强实战能⼒。学习完该项⽬我们应该具备如下能⼒:
1⾯向对象程序设计
2⼯具类二本军校 封装与使⽤写法
3⽂件IO流
4字符串处理
5⼆分法查找
6IP地址的不同形式的使⽤
思路
1程序中读取内容
2解析IP字符串,进⾏结构化处理
3封装⼯具类
4接⼝API
⼊参:IP
出参:归属地
代码开发
读取⽂件
publicstaticList
//节点流对接⽂件
FileInputStreamfis=newFileInputStream(filePath);
//转换为字符并指定字符编码
Readerreader=newInputStreamReader(fis,encoding);
//缓冲流提⾼效率
BufferedReaderbr=newBufferedReader(reader);
//爱国主题手抄报 读取
Stringline=null;
//保存读取的数据
List
while((line=ne())!=null){
//添加到集合中
(line);
}
//关闭
();
returnlineList;
结构化ip地址实体类
*
*
*/
publicclassIPAndLocationPojoimplementsComparable
//衍⽣字段⽤于保存ip对应的long值
privatelongstartIPLong;
privatelongstartIPLong;
privatelongendIPLong;
//起始IP
privateStringstartIP;
//结束IP
privateStringendIP;
//归属地
privateStringlocation;
publicintcompareTo(IPAndLocationPojoo){
longstatus=PLong;
//不能强制转换如果两个值相差2127483647的话转换为int之后得到负数
//return(int)(PLong);
returnstatus>0?1:0;
}
publicIPAndLocationPojo(longstartIPLong,longendIPLong,StringstartIP,
StringendIP,Stringlocation){
super();
//对长整型赋值
PLong=ng(startIP);;
ong=ng(endIP);;
P=startIP;
=endIP;
on=location;
}
publiclonggetStartIPLong(){
returnstartIPLong;
}
publicvoidtStartIPLong(longstartIPLong){
PLong=startIPLong;
}
publiclonggetEndIPLong(){
returnendIPLong;
}
publicvoidtEndIPLong(longendIPLong){
ong=endIPLong;
}
publicStringgetStartIP(){
returnstartIP;
}
publicvoidtStartIP(StringstartIP){
P=startIP;
}
publicStringgetEndIP(){
returnendIP;
}
publicvoidtEndIP(StringendIP){
=endIP;
}
publicStringgetLocation(){
returnlocation;
}
publicvoidtLocation(Stringlocation){
on=location;
}
publicIPAndL凯迪拉克广告语 ocationPojo(){
super();
//TODOAuto-generatedconstructorstub
}
@Override
publicStringtoString(){
return"IPAndLocationPojo[startIPLong="+startIPLong+",endIPLong="
+endIPLong+",startIP="+startIP+",endIP="+endIP
+",location="+location+"]";
}
程序核⼼业务类
*
*
*/
publicclassDataProcessManager{
privatestaticIPAndLocationPojo[]ipAndLocationPojoArray=null;
static{
//⽂件路径
StringipLibrayPath="ip_location_";
Stringencoding="UTF-8";
//保存数据对象
List
try{
//获取数据
ipAndLocationPojos=oList(ipLibrayPath,
encoding);
//转数组并排序
ipAndLocationPojoArray=DataProcessManager
.convertListToArraySort(ipAndLocationPojos);
}catch(IOExceptione){
tackTrace();
}
}
/**
*对外提供的接⼝⼊参是ip出参是归属地
*@paramipAndLocationPojos
*@return
*/
publicstaticStringgetLocation(Stringip){
//⼆分法查找
intindex=ySeach(ipAndLocationPojoArray,ip);
//判断是否找到
if(index==-1){
returnnull;
}el{
returnipAndLocationPojoArray[index].getLocation();
}
}
/**
*⼆分法查找,⼊参是IP和数组,出参是对应的索引,找不到返回-1;
*@paramipAndLocationPojos
*@return
*/
publicstaticintbinaraySeach(IPAnd七夕情人节 LocationPojo[]ipAndLocationPojoArray,
StringtargetIP){
//把IP转换为long
longtargetIPLong=ng(targetIP);
intstartIndex=0;
intendIndex=-1;
intm=(startIndex+endIndex)/2;
/**
*如果⼩于起始IP找前⾯
*
*如果⼤于起始IP找后⾯
*
*如果⼤于等于起始IP且⼩于等于结束IP则说明找到了
*/
while(startIndex<=endIndex){
if(targetIPLong>=ipAndLocationPojoArray[m].getStartIPLong()
&&targetIPLong<=ipAndLocationPojoArray[m].getEndIPLong()){
returnm;
returnm;
}
if(targetIPLong
endIndex=m-1;
}el{
startIndex=m+1;
}
m=(startIndex+endIndex)/2;
}
return-1;
}
//把集合转换为数组并排序
publicstaticIPAndLocationPojo[]convertListArraySort(List
//创建数组
IPAndLocationPojo[]ipAndLocationPojoArray=newIPAndLocationPojo[()];
//转换为数组
y(ipAndLocationPojoArray);
//排序
(ipAndLocationPojoArray);
returnipAndLocationPojoabcc词语大全 Array;
}
//结构化数据集合
publicstaticList
//保存数据对象
List
List
for(Stringstring:lineList){
//判断是否是空⾏
if(string==null||().equals("")){
co借条如何写 ntinue;
}
//分割数组
String[]columnArray=("");
//获取起始ip
StringstartIP=columnArray[0];
//获取结束ip
StringendIP=columnArray[1];
//获取归属地
Stringlocation=columnArray[2];
//封装到对象中
IPAndLocationPojoipAndLocationPojo=newIPAndLocationPojo(startIP,endIP,location);
//添加到集合中
(ipAndLocationPojo);
}
returnipAndLocationPojos;
//⼊⼝
publicclassSystemController{
@SuppressWarnings("resource")
publicsta第二世界大战 ticvoidmain(String[]args){
//接收⽤户输⼊
Scannerscanner=newScanner();
while(true){
n("请输⼊IP地址:");
Stringip=ne();
//查询
longstartTime=tTimeMillis();
Stringlocation=ati判断处女 on(ip);
longendTime=tTimeMillis();
n("耗时:"+(endTime-startTime)+""
+location);
本文发布于:2023-03-18 15:26:57,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/e828c7f3946d83f48ea4c5c37adf3079.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:我的ip地址查询.doc
本文 PDF 下载地址:我的ip地址查询.pdf
留言与评论(共有 0 条评论) |