真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Java獲取手機(jī)號碼歸屬地的實現(xiàn)

遇到一個需求, 需要發(fā)送短信給全球各地的用戶, 但是各大運(yùn)營商的API都是 區(qū)分 國內(nèi) 和 國際, 但是我們只有電話號碼, 只能自己添加一個方法, 判斷號碼的歸屬地, 然后再分別調(diào)用相應(yīng)區(qū)域的API.

成都創(chuàng)新互聯(lián)從2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目網(wǎng)站制作、做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元大廠做網(wǎng)站,已為上家服務(wù),為大廠各地企業(yè)和個人服務(wù),聯(lián)系電話:13518219792

引入依賴包



  com.googlecode.libphonenumber
  libphonenumber
  7.0


  com.googlecode.libphonenumber
  carrier
  1.11


  com.googlecode.libphonenumber
  geocoder
  2.12


工具類方法

package cn.net.jarvis.common.core;

import com.alibaba.fastjson.JSONObject;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;

import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Mao, hongtu1993@sina.cn
 * @version Enter version here..., 5:47 PM 2019/12/16
 */
public class PhoneUtil {
  private static PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();

  private static PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();


  /**
   * 根據(jù)國家代碼和手機(jī)號 判斷手機(jī)號是否有效
   * @param phoneNumber 手機(jī)號碼
   * @param countryCode 國號(區(qū)號)
   * @return true / false
   */
  public static boolean checkPhoneNumber(long phoneNumber, int countryCode) {
    PhoneNumber pn = new PhoneNumber();
    pn.setCountryCode(countryCode);
    pn.setNationalNumber(phoneNumber);
    return phoneNumberUtil.isValidNumber(pn);
  }

  private static Pattern phoneReg = Pattern.compile("\\+(9[976]\\d|8[987530]\\d|6[987]\\d|5[90]\\d|42\\d|3[875]\\d|2[98654321]\\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\\d{1,14}$");

  public static JSONObject getPhoneNumberInfo(String phoneNumber) throws Exception {
    // 正則校驗
    Matcher matcher = phoneReg.matcher(phoneNumber);
    if (!matcher.find()) {
      throw new Exception("The phone number maybe like:" + "+[National Number][Phone number], but got " + phoneNumber);
    }

    Phonenumber.PhoneNumber referencePhonenumber;
    try {
      String language = "CN";
      referencePhonenumber = phoneNumberUtil.parse(phoneNumber, language);
    } catch (NumberParseException e) {
      throw new Exception(e.getMessage());
    }
    String regionCodeForNumber = phoneNumberUtil.getRegionCodeForNumber(referencePhonenumber);

    if (regionCodeForNumber == null) {
      throw new Exception("Missing region code by phone number " + phoneNumber);
    }

    boolean checkSuccess = PhoneUtil.checkPhoneNumber(referencePhonenumber.getNationalNumber(), referencePhonenumber.getCountryCode());
    if (!checkSuccess) {
      throw new Exception("Not an active number:" + phoneNumber);
    }

    String description = geocoder.getDescriptionForNumber(referencePhonenumber, Locale.CHINA);

    int countryCode = referencePhonenumber.getCountryCode();
    long nationalNumber = referencePhonenumber.getNationalNumber();
    JSONObject resultObject = new JSONObject();
    // 區(qū)域編碼 Locale : HK, US, CN ...
    resultObject.put("regionCode", regionCodeForNumber);
    // 國號: 86, 1, 852 ... @link: https://blog.csdn.net/wzygis/article/details/45073327
    resultObject.put("countryCode", countryCode);
    // 去掉+號 和 國號/區(qū)號 后的實際號碼
    resultObject.put("nationalNumber", nationalNumber);
    // 所在地區(qū)描述信息
    resultObject.put("description", description);
    // 去掉+號后的號碼 (用于阿里云發(fā)送短信)
    resultObject.put("number", String.valueOf(countryCode) + nationalNumber);
    resultObject.put("fullNumber", phoneNumber);

    return resultObject;

  }

  public static void main(String[] args) throws Exception {

    // {"number":"85268476749","regionCode":"HK","nationalNumber":68476749,"countryCode":852,"description":"香港","fullNumber":"+85268476749"}
    System.out.println(PhoneUtil.getPhoneNumberInfo("+85268476749").toJSONString());

    // {"number":"16467879865","regionCode":"US","nationalNumber":6467879865,"countryCode":1,"description":"美國","fullNumber":"+16467879865"}
    System.out.println(PhoneUtil.getPhoneNumberInfo("+16467879865").toJSONString());

    // The phone number maybe like:+[National Number][Phone number], but got +86155xxxx331
    System.out.println(PhoneUtil.getPhoneNumberInfo("+86155xxxx331").toJSONString());
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。


分享文章:Java獲取手機(jī)號碼歸屬地的實現(xiàn)
標(biāo)題URL:http://weahome.cn/article/iigchi.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部