Java 识别手机号码归属地、省份、城市、运营商类型
生活随笔
收集整理的這篇文章主要介紹了
Java 识别手机号码归属地、省份、城市、运营商类型
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
maven依賴
<!-- 電話號碼工具類 --><dependency><groupId>com.googlecode.libphonenumber</groupId><artifactId>libphonenumber</artifactId><version>8.12.29</version></dependency><dependency><groupId>com.googlecode.libphonenumber</groupId><artifactId>carrier</artifactId><version>1.155</version></dependency><dependency><groupId>com.googlecode.libphonenumber</groupId><artifactId>geocoder</artifactId><version>2.165</version></dependency><!-- 電話號碼工具類 END -->工具類1
import java.io.*; import java.net.URL; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.charset.StandardCharsets;/*** Created by fengjiajie on 16/10/12.*/ public class PhoneNumberGeo {private static final String[] PHONE_NUMBER_TYPE = {null, "移動", "聯通", "電信", "電信虛擬運營商", "聯通虛擬運營商", "移動虛擬運營商"};private static final int INDEX_SEGMENT_LENGTH = 9;private static final int DATA_FILE_LENGTH_HINT = 1024 * 1024 * 5;private static byte[] dataByteArray;private static int indexAreaOffset;private static int phoneRecordCount;private ByteBuffer byteBuffer;private static synchronized void initData() throws FileNotFoundException {if (dataByteArray == null) {ByteArrayOutputStream byteData = new ByteArrayOutputStream(DATA_FILE_LENGTH_HINT);byte[] buffer = new byte[1024];int readBytesLength;//phone.bat 修改自己的路徑File file = new File("D:\\javaData\\phone.dat");FileInputStream fileInputStream = new FileInputStream(file);// BufferedInputStream inputStream1 = FileUtil.getInputStream(file);try (InputStream inputStream = fileInputStream) {while ((readBytesLength = inputStream.read(buffer)) != -1) {byteData.write(buffer, 0, readBytesLength);}} catch (Exception e) {System.err.println("Can't find phone.dat in classpath");e.printStackTrace();throw new RuntimeException(e);}dataByteArray = byteData.toByteArray();ByteBuffer byteBuffer = ByteBuffer.wrap(dataByteArray);byteBuffer.order(ByteOrder.LITTLE_ENDIAN);int dataVersion = byteBuffer.getInt();indexAreaOffset = byteBuffer.getInt();phoneRecordCount = (dataByteArray.length - indexAreaOffset) / INDEX_SEGMENT_LENGTH;// print data version// System.out.println(dataVersion);// print record count// System.out.println(phoneRecordCount);}}public PhoneNumberGeo() throws FileNotFoundException {initData();byteBuffer = ByteBuffer.wrap(dataByteArray);byteBuffer.order(ByteOrder.LITTLE_ENDIAN);}public PhoneNumberInfo lookup(String phoneNumber) {if (phoneNumber == null || phoneNumber.length() > 11 || phoneNumber.length() < 7) {return null;}int phoneNumberPrefix;try {phoneNumberPrefix = Integer.parseInt(phoneNumber.substring(0, 7));} catch (Exception e) {return null;}int left = 0;int right = phoneRecordCount;while (left <= right) {int middle = (left + right) >> 1;int currentOffset = indexAreaOffset + middle * INDEX_SEGMENT_LENGTH;if (currentOffset >= dataByteArray.length) {return null;}byteBuffer.position(currentOffset);int currentPrefix = byteBuffer.getInt();if (currentPrefix > phoneNumberPrefix) {right = middle - 1;} else if (currentPrefix < phoneNumberPrefix) {left = middle + 1;} else {int infoBeginOffset = byteBuffer.getInt();int phoneType = byteBuffer.get();int infoLength = -1;for (int i = infoBeginOffset; i < indexAreaOffset; ++i) {if (dataByteArray[i] == 0) {infoLength = i - infoBeginOffset;break;}}String infoString = new String(dataByteArray, infoBeginOffset, infoLength, StandardCharsets.UTF_8);String[] infoSegments = infoString.split("\\|");PhoneNumberInfo phoneNumberInfo = new PhoneNumberInfo();phoneNumberInfo.setPhoneNumber(phoneNumber);phoneNumberInfo.setProvince(infoSegments[0]);phoneNumberInfo.setCity(infoSegments[1]);phoneNumberInfo.setZipCode(infoSegments[2]);phoneNumberInfo.setAreaCode(infoSegments[3]);phoneNumberInfo.setPhoneType(PHONE_NUMBER_TYPE[phoneType]);return phoneNumberInfo;}}return null;}}修改phone.dat路徑?
工具類2
import com.google.i18n.phonenumbers.PhoneNumberToCarrierMapper; import com.google.i18n.phonenumbers.PhoneNumberUtil; import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;import java.io.FileNotFoundException; import java.util.Locale;/*** @ClassName: PhoneUtil* @Description:手機號碼歸屬地工具類*/ public class PhoneUtils {private static PhoneNumberGeo phoneNumberGeo;static {try {phoneNumberGeo = new PhoneNumberGeo();} catch (FileNotFoundException e) {e.printStackTrace();}}private static PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();private static PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper.getInstance();private static PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();/*** 直轄市*/private final static String[] MUNICIPALITY = {"北京市", "天津市", "上海市", "重慶市"};/*** 自治區*/private final static String[] AUTONOMOUS_REGION = {"新疆", "內蒙古", "西藏", "寧夏", "廣西"};/*** 根據國家代碼和手機號 判斷手機號是否有效** @param phoneNumber* @param countryCode* @return*/public static boolean checkPhoneNumber(String phoneNumber, String countryCode) {int ccode = Integer.parseInt(countryCode);long phone = Long.parseLong(phoneNumber);PhoneNumber pn = new PhoneNumber();pn.setCountryCode(ccode);pn.setNationalNumber(phone);return phoneNumberUtil.isValidNumber(pn);}/*** 根據國家代碼和手機號 判斷手機運營商** @param phoneNumber* @param countryCode* @return*/public static String getCarrier(String phoneNumber, String countryCode) {int ccode = Integer.parseInt(countryCode);long phone = Long.parseLong(phoneNumber);PhoneNumber pn = new PhoneNumber();pn.setCountryCode(ccode);pn.setNationalNumber(phone);//返回結果只有英文,自己轉成成中文String carrierEn = carrierMapper.getNameForNumber(pn, Locale.ENGLISH);switch (carrierEn) {case "China Mobile":return "移動";case "China Unicom":return "聯通";case "China Telecom":return "電信";default:break;}return "未知";}public static String getGeo(String phoneNumber, String countryCode) {long phone = Long.parseLong(phoneNumber);int country = Integer.parseInt(countryCode);PhoneNumber pn = new PhoneNumber();pn.setCountryCode(country);pn.setNationalNumber(phone); /* geocoder.getDescriptionForNumber()System.out.println(regionCodeForNumber);*/return geocoder.getDescriptionForNumber(pn, Locale.CHINESE);}public static String getProvince(String phoneNumber, String countryCode) {String geo = getGeo(phoneNumber, countryCode);//直轄市for (String val : MUNICIPALITY) {if (geo.equals(val)) {return val;}}// 自治區for (String val : AUTONOMOUS_REGION) {if (geo.startsWith(val)) {return val;}}// 其它String[] splitArr = geo.split("省");if (splitArr != null && splitArr.length == 2) {return splitArr[0];}return "未知";}public static PhoneNumberInfo getPhoneNumberInfo(String mobile) {return phoneNumberGeo.lookup(mobile);}public static String getPhoneType(String mobile) {try {return getPhoneNumberInfo(mobile).getPhoneType();} catch (Exception e) {return "未知";}}public static String getProvince(String mobile) {try {return getPhoneNumberInfo(mobile).getProvince();} catch (Exception e) {return "未知";}}public static String getCity(String mobile) {try {return getPhoneNumberInfo(mobile).getCity();} catch (Exception e) {return "未知";}}public static String getAreaName(String mobile) {try {String province = getProvince(mobile);String city = getCity(mobile);if (province.equals(city)) {return city;}return province + city;} catch (Exception e) {return "未知";}}/* 18250093106 18729283660 15916356311 18741869630 13572028774 15933232795 15935687067 18273049689 13597498763*//*public static void main(String[] args) {System.out.println(getGeo("18250093106", "86"));System.out.println(getCarrier("18250093106", "86"));}*/ } // 直接調用//運營商String phoneType = PhoneUtils.getPhoneType(mobile); //歸屬地String areaName = PhoneUtils.getAreaName(mobile); //城市String city = PhoneUtils.getCity(mobile); //省份String province = PhoneUtils.getProvince(mobile);總結
以上是生活随笔為你收集整理的Java 识别手机号码归属地、省份、城市、运营商类型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot--简单处理高并发d
- 下一篇: 2020前端知识体系(图谱)