判断一个IP是否在指定IP范围内

ContractedBlock.gif ExpandedBlockStart.gif Code
public class IPRegion
    {
        
public static bool IPInRegion(string beginIP, string endIP, string inputIP)
        {
            IPAddress _beginip;
            IPAddress _endip;
            IPAddress _inputip;
            
try
            {
                _beginip 
= IPAddress.Parse(beginIP);
                _endip 
= IPAddress.Parse(endIP);
                _inputip 
= IPAddress.Parse(inputIP);
            }
            
catch (Exception ex)
            {
                ErrorHandler.Handle(ex);
                
return false;
            }
            
if (_beginip == null || _endip == null || _inputip == nullreturn false;

            
byte[] _beginbyte = _beginip.GetAddressBytes();
            Int64 _beginint 
= ConvertIPToInt(_beginbyte);
            
byte[] _endbyte = _endip.GetAddressBytes();
            Int64 _endint 
= ConvertIPToInt(_endbyte);

            
if (_beginint > _endint) return false;

            
byte[] _inputtype = _inputip.GetAddressBytes();
            Int64 _inputint 
= ConvertIPToInt(_inputtype);

            
return (_inputint <= _endint && _inputint >= _beginint);
        }

        
protected static Int64 ConvertIPToInt(byte[] _bytes)
        {
            
if (_bytes.Length != 4return 0;
            
string _intstring = "";
            
for (int i = 0; i < _bytes.Length; i++)
            {
                _intstring 
+= FormatIPPart(_bytes[i].ToString());
            }
            Int64 _outputint;
            
return !Int64.TryParse(_intstring, out _outputint) ? 0 : _outputint;
        }

        
protected static string FormatIPPart(string _ippart)
        {
            Regex _regx 
= new Regex(@"^\d{1,3}$");
            
if (!_regx.IsMatch(_ippart)) return "000";
            
while (_ippart.Length < 3)
            {
                _ippart 
= "0" + _ippart;
            }
            
return _ippart;
        }
    }

转载于:https://www.cnblogs.com/hotsam/archive/2008/10/30/1322889.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
判断IP地址是否指定地区范围内,可以使用IP地址库和Java中的IP地址操作类。以下是一个简单的示例: ```java import java.net.InetAddress; import java.net.UnknownHostException; // 使用IP地址库来获取IP地址所属的地区 import com.maxmind.geoip2.DatabaseReader; import com.maxmind.geoip2.exception.GeoIp2Exception; import com.maxmind.geoip2.model.CityResponse; import com.maxmind.geoip2.record.Country; import com.maxmind.geoip2.record.Subdivision; public class IpRegionChecker { // GeoLite2-City.mmdb为IP地址库文件,可以从MaxMind官网下载 private static final String IP_DATABASE_PATH = "GeoLite2-City.mmdb"; private DatabaseReader reader; public IpRegionChecker() throws Exception { reader = new DatabaseReader.Builder(getClass().getResourceAsStream(IP_DATABASE_PATH)).build(); } public String getRegion(String ipAddress) throws UnknownHostException, GeoIp2Exception { InetAddress inetAddress = InetAddress.getByName(ipAddress); CityResponse response = reader.city(inetAddress); Country country = response.getCountry(); Subdivision subdivision = response.getMostSpecificSubdivision(); String region = subdivision.getName(); return region; } public static void main(String[] args) throws Exception { IpRegionChecker checker = new IpRegionChecker(); String ipAddress = "192.168.1.100"; // 要判断IP地址 String region = checker.getRegion(ipAddress); // 获取该IP地址所属的地区 System.out.println("IP地址 " + ipAddress + " 所属地区为:" + region); } } ``` 需要使用MaxMind提供的IP地址库,可以从其官网下载。该示例使用了GeoLite2-City.mmdb文件,可以在构造函数中指定IP地址库文件路径。在`getRegion()`方法中,首先使用InetAddress类将IP地址转换为InetAddress对象,然后使用DatabaseReader类读取IP地址库文件,获取该IP地址所属的国家和地区信息。最后返回该IP地址所属的地区名称。可以根据需要修改该示例来满足不同的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值