IP查找所属网段
2024-10-21 07:49:33
最近同学接到阿里面试题
package io.guangsoft.analysis; /*
数据文件:
1.1.1.0/24,123
1.1.2.0/28,345
1.2.0.0/16,789
*/
public interface Finder {
//完成初始化动作
boolean loadFile(String fileName);
//查找具体IP所在IP网段的键值,未找到返回-1;比如如果输入样例数据文件,调用find("1,1,1,1"),返回123
int find(String ipStr);
}
搜索一番解答如下
package io.guangsoft.analysis; import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map; import org.apache.commons.net.util.SubnetUtils;
import org.apache.commons.net.util.SubnetUtils.SubnetInfo; public class FinderImpl implements Finder { private Map<String, String> dataMap = new HashMap<String, String>(); @Override
public boolean loadFile(String fileName) {
File file = new File(this.getClass().getClassLoader().getResource(fileName).getFile());
try {
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while(true) {
String nextLine = bufferedReader.readLine();
if(nextLine == null) {
break;
} else {
String str[] = nextLine.split(",");
dataMap.put(str[0], str[1]);
}
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
} @Override
public int find(String ip) {
for(String network : dataMap.keySet()) {
boolean bingo = isInRange(ip, network);
if(bingo) {
return Integer.parseInt(dataMap.get(network));
}
}
return -1;
} //核心代码,检索IP所属网段
public boolean isInRange(String ip, String network) {
String[] ips = ip.split("\\.");
int ipAddr = (Integer.parseInt(ips[0]) << 24)
| (Integer.parseInt(ips[1]) << 16)
| (Integer.parseInt(ips[2]) << 8)
| Integer.parseInt(ips[3]);
int type = Integer.parseInt(network.replaceAll(".*/", ""));
int mask = 0xFFFFFFFF << (32 - type);
String networkIp = network.replaceAll("/.*", "");
String[] networkIps = networkIp.split("\\.");
int networkIpAddr = (Integer.parseInt(networkIps[0]) << 24)
| (Integer.parseInt(networkIps[1]) << 16)
| (Integer.parseInt(networkIps[2]) << 8)
| Integer.parseInt(networkIps[3]);
return (ipAddr & mask) == (networkIpAddr & mask);
} //也可以使用apache的net工具类
public boolean isInRange2(String ip, String network) {
SubnetInfo subnetInfo = new SubnetUtils(network).getInfo();
return subnetInfo.isInRange(ip);
} public static void main(String args[]) {
FinderImpl finder = new FinderImpl();
if(finder.loadFile("data.txt")) {
int num = finder.find("1.1.1.1");
System.out.println(num);
}
} }
最新文章
- 前端MVVM框架avalon揭秘 - 双向绑定原理
- Record:Handle onClick for our custom LinearLayout for Gallery-like HorizontalScrollView
- log4net--帮助程序员将日志信息输出到各种目标(控制台、文件、数据库等)的工具
- SpringBoot入门 一 构建简单工程
- Cacti优化之spine轮询器
- Windows Server 2012 安装dll到GAC
- Hibernate 框架基本知识
- android4.0下如何判断手机是否有底部物理按键(menu物理按键)
- AsParallel 用法
- Babel初体验
- 组合 Lucas定理
- This version of the rendering library is more recent than your version of ADT plug-in. Please update
- Ubuntu系统下配置IP地址方法介绍
- adb devices 报错处理
- mysql导出导入数据
- 基于Spring Boot和Shiro的后台管理系统FEBS
- Windows 服务安装教程
- java痛苦学习之路[十]--日常问题汇总
- 详解Object.constructor
- pytest文档25-conftest.py作用范围