直接上代码,需要市区县可自己解析
String areaName = addressUtil.getPosition(longitude, latitude);
package com.skyable.device.utils.velicle;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Administrator
*/
@Component
public class AddressUtil {
@Autowired
private JsonUtil jsonUtil;
private static final List
package com.skyable.device.utils.velicle;
/**
* @author Administrator
*/
public class CoordTransform {
private static final double X_PI = 52.359877559829883;
private static final double PI = 3.141592653589793;
private static final double A = 6378245.0;
private static final double EE = 0.006693421622965943;
public static double getDistance(double x1, double y1, double x2, double y2) {
double x = (x1 - x2) * PI * A *
Math.cos((y1 + y2) / 2.0 * PI / 180.0) / 180.0;
double y = (y2 - y1) * PI * A / 180.0;
return Math.sqrt(x * x + y * y);
}
}
package com.skyable.device.utils.velicle;
public class IPEntry {
public String beginIp;
public String endIp;
public String country;
public String area;
public IPEntry() {
this.beginIp = (this.endIp = this.country = this.area = "");
}
@Override
public String toString() {
return this.area + " " + this.country + "IP范围:" + this.beginIp + "-" +
this.endIp;
}
}
package com.skyable.device.utils.velicle;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteOrder;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.*;
public class IPSeeker {
private static final String IP_FILE = PropertiesUtil.getProperty("IP_DATA_URL");
private static final int IP_RECORD_LENGTH = 7;
private static final byte AREA_FOLLOWED = 1;
private static final byte NO_AREA = 2;
private static Hashtable ipCache;
private static RandomAccessFile ipFile;
private static MappedByteBuffer mbb;
private static IPSeeker instance = new IPSeeker();
private static long ipBegin;
private static long ipEnd;
private static IPLocation loc;
private static byte[] buf;
private static byte[] b4;
private static byte[] b3;
private IPSeeker() {
ipCache = new Hashtable();
loc = new IPLocation();
buf = new byte[100];
b4 = new byte[4];
b3 = new byte[3];
try {
ipFile = new RandomAccessFile(IP_FILE, "r");
} catch (FileNotFoundException e) {
System.out.println(IP_FILE);
System.out.println("IP地址信息文件没有找到,IP显示功能将无法使用");
ipFile = null;
}
if (ipFile != null) {
try {
ipBegin = readLong4(0L);
ipEnd = readLong4(4L);
if ((ipBegin == -1L) || (ipEnd == -1L)) {
ipFile.close();
ipFile = null;
}
} catch (IOException e) {
System.out.println("IP地址信息文件格式有错误,IP显示功能将无法使用");
ipFile = null;
}
}
}
public static IPSeeker getInstance() {
return instance;
}
public List getIPEntriesDebug(String s) {
List ret = new ArrayList();
long endOffset = ipEnd + 4L;
for (long offset = ipBegin + 4L; offset <= endOffset; offset += 7L) {
long temp = readLong3(offset);
if (temp != -1L) {
IPLocation loc = getIPLocation(temp);
if ((loc.country.indexOf(s) != -1) || (loc.area.indexOf(s) != -1)) {
IPEntry entry = new IPEntry();
entry.country = loc.country;
entry.area = loc.area;
readIP(offset - 4L, b4);
entry.beginIp = Utils.getIpStringFromBytes(b4);
readIP(temp, b4);
entry.endIp = Utils.getIpStringFromBytes(b4);
ret.add(entry);
}
}
}
return ret;
}
public List getIPEntries(String s) {
List ret = new ArrayList();
try {
if (mbb == null) {
FileChannel fc = ipFile.getChannel();
mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0L, ipFile.length());
mbb.order(ByteOrder.LITTLE_ENDIAN);
}
int endOffset = (int) ipEnd;
for (int offset = (int) ipBegin + 4; offset <= endOffset; offset += 7) {
int temp = readInt3(offset);
if (temp != -1) {
IPLocation loc = getIPLocation(temp);
if ((loc.country.indexOf(s) != -1) ||
(loc.area.indexOf(s) != -1)) {
IPEntry entry = new IPEntry();
entry.country = loc.country;
entry.area = loc.area;
readIP(offset - 4, b4);
entry.beginIp = Utils.getIpStringFromBytes(b4);
readIP(temp, b4);
entry.endIp = Utils.getIpStringFromBytes(b4);
ret.add(entry);
}
}
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
return ret;
}
private int readInt3(int offset) {
mbb.position(offset);
return mbb.getInt() & 0xFFFFFF;
}
private int readInt3() {
return mbb.getInt() & 0xFFFFFF;
}
public static String getCountry(byte[] ip) {
if (ipFile == null) {
return "错误的IP数据库文件";
}
String ipStr = Utils.getIpStringFromBytes(ip);
if (ipCache.containsKey(ipStr)) {
IPLocation loc = (IPLocation) ipCache.get(ipStr);
return loc.country;
}
IPLocation loc = getIPLocation(ip);
ipCache.put(ipStr, loc.getCopy());
return loc.country;
}
public static String getCountry(String ip) {
return getCountry(Utils.getIpByteArrayFromString(ip));
}
public String getArea(byte[] ip) {
if (ipFile == null) {
return "错误的IP数据库文件";
}
String ipStr = Utils.getIpStringFromBytes(ip);
if (ipCache.containsKey(ipStr)) {
IPLocation loc = (IPLocation) ipCache.get(ipStr);
return loc.area;
}
IPLocation loc = getIPLocation(ip);
ipCache.put(ipStr, loc.getCopy());
return loc.area;
}
public String getArea(String ip) {
return getArea(Utils.getIpByteArrayFromString(ip));
}
private static IPLocation getIPLocation(byte[] ip) {
IPLocation info = null;
long offset = locateIP(ip);
if (offset != -1L) {
info = getIPLocation(offset);
}
if (info == null) {
info.country = "未知国家";
info.area = "未知地区";
}
return info;
}
private long readLong4(long offset) {
long ret = 0L;
try {
ipFile.seek(offset);
ret |= ipFile.readByte() & 0xFF;
ret |= ipFile.readByte() << 8 & 0xFF00;
ret |= ipFile.readByte() << 16 & 0xFF0000;
return ret | ipFile.readByte() << 24 & 0xFF000000;
} catch (IOException e) {
}
return -1L;
}
private static long readLong3(long offset) {
long ret = 0L;
try {
ipFile.seek(offset);
ipFile.readFully(b3);
ret |= b3[0] & 0xFF;
ret |= b3[1] << 8 & 0xFF00;
return ret | b3[2] << 16 & 0xFF0000;
} catch (IOException e) {
}
return -1L;
}
private static long readLong3() {
long ret = 0L;
try {
ipFile.readFully(b3);
ret |= b3[0] & 0xFF;
ret |= b3[1] << 8 & 0xFF00;
return ret | b3[2] << 16 & 0xFF0000;
} catch (IOException e) {
}
return -1L;
}
private static void readIP(long offset, byte[] ip) {
try {
ipFile.seek(offset);
ipFile.readFully(ip);
byte temp = ip[0];
ip[0] = ip[3];
ip[3] = temp;
temp = ip[1];
ip[1] = ip[2];
ip[2] = temp;
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
private void readIP(int offset, byte[] ip) {
mbb.position(offset);
mbb.get(ip);
byte temp = ip[0];
ip[0] = ip[3];
ip[3] = temp;
temp = ip[1];
ip[1] = ip[2];
ip[2] = temp;
}
private static int compareIP(byte[] ip, byte[] beginIp) {
for (int i = 0; i < 4; i++) {
int r = compareByte(ip[i], beginIp[i]);
if (r != 0) {
return r;
}
}
return 0;
}
private static int compareByte(byte b1, byte b2) {
if ((b1 & 0xFF) > (b2 & 0xFF)) {
return 1;
}
if ((b1 ^ b2) == 0) {
return 0;
}
return -1;
}
private static long locateIP(byte[] ip) {
long m = 0L;
readIP(ipBegin, b4);
int r = compareIP(ip, b4);
if (r == 0) {
return ipBegin;
}
if (r < 0) {
return -1L;
}
long i = ipBegin;
for (long j = ipEnd; i < j; ) {
m = getMiddleOffset(i, j);
readIP(m, b4);
r = compareIP(ip, b4);
if (r > 0) {
i = m;
} else if (r < 0) {
if (m == j) {
j -= 7L;
m = j;
} else {
j = m;
}
} else {
return readLong3(m + 4L);
}
}
m = readLong3(m + 4L);
readIP(m, b4);
r = compareIP(ip, b4);
if (r <= 0) {
return m;
}
return -1L;
}
private static long getMiddleOffset(long begin, long end) {
long records = (end - begin) / 7L;
records >>= 1;
if (records == 0L) {
records = 1L;
}
return begin + records * 7L;
}
private static IPLocation getIPLocation(long offset) {
try {
ipFile.seek(offset + 4L);
byte b = ipFile.readByte();
if (b == 1) {
long countryOffset = readLong3();
ipFile.seek(countryOffset);
b = ipFile.readByte();
if (b == 2) {
loc.country = readString(readLong3());
ipFile.seek(countryOffset + 4L);
} else {
loc.country = readString(countryOffset);
}
loc.area = readArea(ipFile.getFilePointer());
} else if (b == 2) {
loc.country = readString(readLong3());
loc.area = readArea(offset + 8L);
} else {
loc.country = readString(ipFile.getFilePointer() - 1L);
loc.area = readArea(ipFile.getFilePointer());
}
return loc;
} catch (IOException e) {
}
return null;
}
private IPLocation getIPLocation(int offset) {
mbb.position(offset + 4);
byte b = mbb.get();
if (b == 1) {
int countryOffset = readInt3();
mbb.position(countryOffset);
b = mbb.get();
if (b == 2) {
loc.country = readString(readInt3());
mbb.position(countryOffset + 4);
} else {
loc.country = readString(countryOffset);
}
loc.area = readArea(mbb.position());
} else if (b == 2) {
loc.country = readString(readInt3());
loc.area = readArea(offset + 8);
} else {
loc.country = readString(mbb.position() - 1);
loc.area = readArea(mbb.position());
}
return loc;
}
private static String readArea(long offset)
throws IOException {
ipFile.seek(offset);
byte b = ipFile.readByte();
if ((b == 1) || (b == 2)) {
long areaOffset = readLong3(offset + 1L);
if (areaOffset == 0L) {
return "未知地区";
}
return readString(areaOffset);
}
return readString(offset);
}
private String readArea(int offset) {
mbb.position(offset);
byte b = mbb.get();
if ((b == 1) || (b == 2)) {
int areaOffset = readInt3();
if (areaOffset == 0) {
return "未知地区";
}
return readString(areaOffset);
}
return readString(offset);
}
private static String readString(long offset) {
try {
ipFile.seek(offset);
int i = 0;
for (buf[i] = ipFile.readByte(); buf[i] != 0; ) {
buf[(++i)] = ipFile
.readByte();
}
if (i != 0) {
return Utils.convertBytesToString(buf, 0, i, "GBK");
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
return "";
}
private String readString(int offset) {
try {
mbb.position(offset);
int i = 0;
for (buf[i] = mbb.get(); buf[i] != 0; buf[(++i)] = mbb.get()) {
if (i != 0) {
return Utils.convertBytesToString(buf, 0, i, "GBK");
}
}
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
return "";
}
public String getAddress(String ip) {
String country = getCountry(ip).equals(" CZ88.NET") ? "null" :
getCountry(ip);
String area = getArea(ip).equals(" CZ88.NET") ? "null" : getArea(ip);
String address = country + "|" + area;
return address.trim();
}
public static String getAddressByCity(String ip) {
String country = getCountry(ip).equals(" CZ88.NET") ? "null" :
getCountry(ip);
String address = country;
return address.trim();
}
public static Map jqCity(String str) {
String shortname = "";
String areaname = "";
Map map = new HashMap();
boolean status = str.contains("省");
if (status) {
String[] splitstr = str.split("省");
shortname = splitstr[0] + "省";
areaname = splitstr[1];
if (areaname.contains("市")) {
areaname = areaname.split("市")[0] + "市";
}
}
boolean xj = str.contains("新疆");
if (xj) {
String[] splitstr = str.split("新疆");
for (String string : splitstr) {
System.out.println(string);
}
shortname = "新疆";
areaname = splitstr[1];
}
boolean xz = str.contains("西藏");
if (xz) {
String[] splitstr = str.split("西藏");
for (String string : splitstr) {
System.out.println(string);
}
shortname = "西藏";
areaname = splitstr[1];
}
boolean nmg = str.contains("内蒙古");
if (nmg) {
String[] splitstr = str.split("内蒙古");
for (String string : splitstr) {
System.out.println(string);
}
shortname = "内蒙古";
areaname = splitstr[1];
}
boolean nx = str.contains("宁夏");
if (nx) {
String[] splitstr = str.split("宁夏");
for (String string : splitstr) {
System.out.println(string);
}
shortname = "宁夏";
areaname = splitstr[1];
}
boolean gx = str.contains("广西");
if (gx) {
String[] splitstr = str.split("广西");
for (String string : splitstr) {
System.out.println(string);
}
shortname = "广西";
areaname = splitstr[1];
}
boolean bjs = str.contains("北京市");
boolean tjs = str.contains("天津市");
boolean shs = str.contains("上海市");
boolean cqs = str.contains("重庆市");
boolean xg = str.contains("香港");
boolean am = str.contains("澳门");
if ((am) || (xg) || (cqs) || (shs) || (tjs) || (bjs)) {
System.out.println(str);
shortname = str;
areaname = str;
}
map.put("shortname", shortname);
map.put("areaname", areaname);
return map;
}
public static void main(String[] args) {
IPSeeker is = new IPSeeker();
System.out.println(is.getAddress("119.123.225.83"));
System.out.println(is.getAddress("14.215.177.39"));
System.out.println(is.getAddress("113.78.255.221"));
System.out.println(is.getAddress("182.34.16.127"));
System.out.println(is.getAddress("180.118.240.225"));
System.out.println(is.getAddress("119.29.252.90"));
System.out.println(is.getAddress("61.128.101.255"));
System.out.println(is.getAddress("110.157.255.255"));
System.out.println(is.getAddress("220.182.44.228"));
System.out.println(is.getAddress("1.183.255.255"));
System.out.println(is.getAddress("123.125.71.38"));
System.out.println(is.getAddress("203.186.145.250"));
System.out.println(is.getAddress("111.113.255.255"));
System.out.println(is.getAddress("180.143.255.255"));
}
private class IPLocation {
public String country;
public String area;
public IPLocation() {
this.country = (this.area = "");
}
public IPLocation getCopy() {
IPLocation ret = new IPLocation();
ret.country = this.country;
ret.area = this.area;
return ret;
}
}
}
package com.skyable.device.utils.velicle;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.util.*;
public class JsonToMap {
public static JsonObject parseJson(String json) {
JsonParser parser = new JsonParser();
return parser.parse(json).getAsJsonObject();
}
public static Map toMap(String json) {
return toMap(parseJson(json));
}
public static Map toMap(JsonObject json) {
Map map = new HashMap<>();
Set> entrySet = json.entrySet();
for (Map.Entry entry : entrySet) {
String key = entry.getKey();
JsonElement value = entry.getValue();
if (value.isJsonArray()) {
map.put(key, toList(value.getAsJsonArray()));
} else if (value.isJsonObject()) {
map.put(key, toMap(value.getAsJsonObject()));
} else {
map.put(key, value);
}
}
return map;
}
public static List
package com.skyable.device.utils.velicle;
import com.google.gson.*;
import com.google.gson.stream.JsonReader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Component
public class JsonUtil {
@Value("${vehicle.jsonFile}")
private String filePath;
public List
package com.skyable.device.utils.velicle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
public class PropertiesUtil {
private static final Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
private static Properties props;
static {
// 更具描述性的文件名
String fileName = "application.test";
props = new Properties();
try (InputStreamReader reader = new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName), "UTF-8")) {
props.load(reader);
} catch (IOException e) {
logger.error("配置文件读取异常", e);
}
}
public static String getProperty(String key) {
String value = props.getProperty(key.trim());
return value != null ? value.trim() : null;
}
public static String getProperty(String key, String defaultValue) {
String value = props.getProperty(key.trim());
return value != null ? value.trim() : defaultValue;
}
}
package com.skyable.device.utils.velicle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException;
import java.util.StringTokenizer;
/**
* @author Administrator
*/
public class Utils {
private static final Logger logger = LoggerFactory.getLogger(Utils.class);
public static byte[] getIpByteArrayFromString(String ip) {
byte[] ret = new byte[4];
StringTokenizer st = new StringTokenizer(ip, ".");
try {
ret[0] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF);
ret[1] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF);
ret[2] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF);
ret[3] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF);
} catch (Exception e) {
logger.error("Error while getting IP byte array from string: {}", e.getMessage());
}
return ret;
}
public static String convertString(String input, String sourceEncoding, String targetEncoding) {
try {
return new String(input.getBytes(sourceEncoding), targetEncoding);
} catch (UnsupportedEncodingException e) {
logger.error("Error while converting string: {}", e.getMessage());
}
return input;
}
public static String convertBytesToString(byte[] byteArray, String encoding) {
try {
return new String(byteArray, encoding);
} catch (UnsupportedEncodingException e) {
logger.error("Error while converting bytes to string: {}", e.getMessage());
}
return new String(byteArray);
}
public static String convertBytesToString(byte[] byteArray, int offset, int length, String encoding) {
try {
return new String(byteArray, offset, length, encoding);
} catch (UnsupportedEncodingException e) {
logger.error("Error while converting bytes to string: {}", e.getMessage());
}
return new String(byteArray, offset, length);
}
public static String getIpStringFromBytes(byte[] ipBytes) {
StringBuilder sb = new StringBuilder();
sb.append(ipBytes[0] & 0xFF);
sb.append('.');
sb.append(ipBytes[1] & 0xFF);
sb.append('.');
sb.append(ipBytes[2] & 0xFF);
sb.append('.');
sb.append(ipBytes[3] & 0xFF);
return sb.toString();
}
}