1.pom坐标
<dependency><groupId>com.googlecode.libphonenumber</groupId><artifactId>geocoder</artifactId><version>2.205</version></dependency>
2.代码
package test;import com.alibaba.excel.util.StringUtils;
import com.google.i18n.phonenumbers.Phonenumber;
import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;import java.util.Locale;/*** 查询手机号码的归属地*/
public class SelectHomePlace {//加载归属地依赖信息public static final PhoneNumberOfflineGeocoder GEOCODER = PhoneNumberOfflineGeocoder.getInstance();//查询国内的手机号码归属地public static final int COUNTRY_CODE = 86;/*** 查询手机号码归属地** @param phoneNum 手机号码* @return 号码归属地*/public static String getPhoneNumAttribution(String phoneNum) {if (StringUtils.isNotBlank(phoneNum) && phoneNum.length() == 11) {try {long phone = Long.parseLong(phoneNum);Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();pn.setCountryCode(COUNTRY_CODE);pn.setNationalNumber(phone);return GEOCODER.getDescriptionForNumber(pn, Locale.CHINESE);} catch (Exception e) {return "未知";}}return "未知";}public static void main(String[] args) {String phoneNumAttribution = getPhoneNumAttribution("15555520934");System.out.println(phoneNumAttribution);}
}
3.运行结果