依赖:
<dependency> <groupId>com.googlecode.libphonenumber</groupId> <artifactId>libphonenumber</artifactId> <version>8.13.26</version> </dependency> <dependency> <groupId>com.googlecode.libphonenumber</groupId> <artifactId>carrier</artifactId> <version>1.210</version> </dependency> <dependency> <groupId>com.googlecode.libphonenumber</groupId> <artifactId>geocoder</artifactId> <version>2.220</version> </dependency>
工具类:
import com.alibaba.fastjson2.JSONObject;
import com.google.i18n.phonenumbers.PhoneNumberToCarrierMapper;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;
import java.util.Locale;
/**
* @author: gs
* @description: 基于 google 的 libphonenumber 将手机号转成地区及供应商信息
**/
public class PhoneToRegionUtil {
/**
* 手机号基本工具类
*/
private final static PhoneNumberUtil PHONE_NUMBER_UTIL = PhoneNumberUtil.getInstance();
/**
* 运营商
*/
private final static PhoneNumberToCarrierMapper CARRIER_MAPPER = PhoneNumberToCarrierMapper.getInstance();
/**
*
*/
private final static PhoneNumberOfflineGeocoder GEO_CODER = PhoneNumberOfflineGeocoder.getInstance();
/**
* 验证当前手机号是否有效
* @param phone 手机号
* @return 校验结果
*/
public static boolean isValidNumber(String phone){
return PHONE_NUMBER_UTIL.isValidNumber(getPhoneNumber(phone));
}
/**
* 获取手机号运营商
* @param phone 手机号
* @return 运营商
*/
public static String getPhoneCarrier(String phone){
return isValidNumber(phone) ? CARRIER_MAPPER.getNameForNumber(getPhoneNumber(phone), Locale.CHINA) : "";
}
/**
* 获取手机号归属地
* @param phone 手机号
* @return 归属地
*/
public static String getRegionInfoByPhone(String phone){
return isValidNumber(phone) ? GEO_CODER.getDescriptionForNumber(getPhoneNumber(phone),Locale.CHINESE) : "";
}
/**
* 生成PhoneNumber
* @param phone 手机号
* @return PhoneNumber
*/
private static Phonenumber.PhoneNumber getPhoneNumber(String phone){
Phonenumber.PhoneNumber phoneNumber = new Phonenumber.PhoneNumber();
// 国际长途电话区号 国内 86
phoneNumber.setCountryCode(86);
phoneNumber.setNationalNumber(Long.parseLong(phone));
return phoneNumber;
}
/**
* 获取手机号的归属信息:运营商,归属地
* @param phone 手机号
* @return 归属信息
*/
public static JSONObject getPhoneInfo(String phone){
JSONObject affiliation = new JSONObject();
affiliation.put("phone",phone);
affiliation.put("carrier",getPhoneCarrier(phone));
affiliation.put("region",getRegionInfoByPhone(phone));
return affiliation;
}
}
示例:
参考:
国际长途电话区号表_百度百科 (baidu.com)
国际手机号码有效性校验 | 嘎里三分熟 (jetchen.cn)
【编程开发】之国际号码校验工具 Libphonenumber-CSDN博客