fengjiajie / phone-number-geo Goto Github PK
View Code? Open in Web Editor NEW手机号码归属地本地解析(Java)
手机号码归属地本地解析(Java)
我在使用你的代码在tomcat下运行时读取的文件长度不正确,导致无法正确解析文件。
读取第一个字节时(版本号)是正确的但是第二个字节开始就不对了。
我观察发现貌似是classloader不一样(废话)。
然后导致读取的文件长度发生了变化。
我测试过将文件复制回来使用你的代码读取也是正确的所有应该不是我复制时导致的文件增长。
我使用了svn不知道是不是svn给这个文件添加了什么。。
PhoneNumberGeo look方法中, 从81行开始有多处的从bytebuffer中读取数据, 而buffer中的position的位置会随之变化, 多线程下不是线程安全的, 需要加锁处理, 如下
synchronized (this) {
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 = null;
try {
infoString = new String(dataByteArray, infoBeginOffset, infoLength, "UTF-8");
} catch (UnsupportedEncodingException e) {
log.error("PhoneNumberGeoService UnsupportedEncodingException ", e);
}
PhoneDataInfo phoneDataInfo = new PhoneDataInfo();
if (infoString != null) {
String[] infoSegments = infoString.split("\\|");
phoneDataInfo.setProvince(infoSegments[0]);
phoneDataInfo.setCity(infoSegments[1]);
phoneDataInfo.setZipCode(infoSegments[2]);
phoneDataInfo.setAreaCode(infoSegments[3]);
}
phoneDataInfo.setPhoneNumber(phoneNumber);
phoneDataInfo.setPhoneType(numberType[phoneType]);
return phoneDataInfo;
}
}
接口都免费调用,很适合个人开发者
https://api-ltc1.doc.coding.io/
代码示例:
PhoneNumberInfo found = phoneNumberLookup.lookup(phone).orElse(getDefaultValue(phone));
执行此方法时,抛出异常:
java.nio.ByteBuffer.position(I)Ljava/nio/ByteBuffer;
临时解决办法:获取源码,使用jdk8重新编译即可。
可能原因:已发布到重要仓库的包使用jdk1.8以上版本编译。
你是把dat文件的数据解析到内存里然后用字节二分查找获取归属地的吗
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.