格式化代码
This commit is contained in:
parent
63be3041b1
commit
db0d0d9052
@ -3,8 +3,6 @@ package com.xkrs.utils;
|
|||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import net.sf.json.JSONObject;
|
|
||||||
import io.micrometer.core.instrument.util.StringUtils;
|
|
||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.HttpStatus;
|
import org.apache.http.HttpStatus;
|
||||||
import org.apache.http.client.ClientProtocolException;
|
import org.apache.http.client.ClientProtocolException;
|
||||||
@ -17,113 +15,26 @@ import org.apache.http.impl.client.HttpClients;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import javax.net.ssl.*;
|
import java.io.BufferedReader;
|
||||||
import java.io.*;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.security.cert.CertificateException;
|
|
||||||
import java.security.cert.X509Certificate;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据经纬度获取地址:省 市 区 位置名称
|
|
||||||
* @author XinYi Song
|
|
||||||
*/
|
|
||||||
public class AddressUtils {
|
public class AddressUtils {
|
||||||
|
|
||||||
/*public static String getLatAndLng(String lat, String lng) {
|
|
||||||
String key = "O7QBZ-ZYDKI-EMKGN-53UHG-5XSJF-AAFBP";
|
|
||||||
try {
|
|
||||||
String hsUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + lat + "," + lng + "&key=" + key + "&get_poi=1";
|
|
||||||
|
|
||||||
URL url;
|
|
||||||
|
|
||||||
url = new URL(hsUrl);
|
|
||||||
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
|
||||||
// 提交模式
|
|
||||||
con.setRequestMethod("GET");
|
|
||||||
X509TrustManager xtm = new X509TrustManager() {
|
|
||||||
@Override
|
|
||||||
public X509Certificate[] getAcceptedIssuers() {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void checkServerTrusted(X509Certificate[] arg0, String arg1)
|
|
||||||
throws CertificateException {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void checkClientTrusted(X509Certificate[] arg0, String arg1)
|
|
||||||
throws CertificateException {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
TrustManager[] tm = {xtm};
|
|
||||||
|
|
||||||
SSLContext ctx = SSLContext.getInstance("TLS");
|
|
||||||
ctx.init(null, tm, null);
|
|
||||||
|
|
||||||
con.setSSLSocketFactory(ctx.getSocketFactory());
|
|
||||||
con.setHostnameVerifier(new HostnameVerifier() {
|
|
||||||
@Override
|
|
||||||
public boolean verify(String arg0, SSLSession arg1) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
InputStream inStream = con.getInputStream();
|
|
||||||
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
|
||||||
byte[] buffer = new byte[1024];
|
|
||||||
int len = 0;
|
|
||||||
while ((len = inStream.read(buffer)) != -1) {
|
|
||||||
outStream.write(buffer, 0, len);
|
|
||||||
}
|
|
||||||
//网页的二进制数据
|
|
||||||
byte[] b = outStream.toByteArray();
|
|
||||||
outStream.close();
|
|
||||||
inStream.close();
|
|
||||||
String rtn = new String(b, "utf-8");
|
|
||||||
if (StringUtils.isNotBlank(rtn)) {
|
|
||||||
JSONObject object = JSONObject.fromObject(rtn);
|
|
||||||
if (object != null) {
|
|
||||||
if (object.has("status") && object.getInt("status") == 0) {
|
|
||||||
JSONObject result = JSONObject.fromObject(object.get("result"));
|
|
||||||
if (result != null) {
|
|
||||||
JSONObject addressComponent = JSONObject.fromObject(result.get("address_component"));
|
|
||||||
if (addressComponent != null) {
|
|
||||||
String province = (String) addressComponent.get("province");
|
|
||||||
String city = (String) addressComponent.get("city");
|
|
||||||
String district = (String) addressComponent.get("district");
|
|
||||||
String street = (String) addressComponent.get("street");
|
|
||||||
String street_number = (String) addressComponent.get("street_number");
|
|
||||||
String address = province + city + district + street + street_number;
|
|
||||||
return address;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
|
private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
|
||||||
private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
|
private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36";
|
||||||
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取指定经纬度的地理位置
|
* 获取指定经纬度的地理位置
|
||||||
|
*
|
||||||
* @param latitude 纬度
|
* @param latitude 纬度
|
||||||
* @param longitude 经度
|
* @param longitude 经度
|
||||||
* @return
|
* @return
|
||||||
@ -155,8 +66,10 @@ public class AddressUtils {
|
|||||||
String fireAddress = province + city + county + road + address;
|
String fireAddress = province + city + county + road + address;
|
||||||
return fireAddress;
|
return fireAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据经纬度获取位置信息
|
* 根据经纬度获取位置信息
|
||||||
|
*
|
||||||
* @param latitude 纬度
|
* @param latitude 纬度
|
||||||
* @param longitude 经度
|
* @param longitude 经度
|
||||||
* @return
|
* @return
|
||||||
@ -177,15 +90,10 @@ public class AddressUtils {
|
|||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
log.error("转json失败,{}", (Object) e.getStackTrace());
|
log.error("转json失败,{}", (Object) e.getStackTrace());
|
||||||
}
|
}
|
||||||
String url = String.format("http://api.tianditu.gov.cn/geocoder?type=geocode&tk=5a1d34815475f88e6d8802da6be832ae&postStr=%s",
|
String url = String.format("http://api.tianditu.gov.cn/geocoder?type=geocode&tk=5a1d34815475f88e6d8802da6be832ae&postStr=%s", paramStr);
|
||||||
paramStr);
|
|
||||||
// 创建http对象
|
// 创建http对象
|
||||||
RequestConfig defaultRequestConfig = RequestConfig.custom()
|
RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).setConnectionRequestTimeout(60000).build();
|
||||||
.setSocketTimeout(60000).setConnectTimeout(60000)
|
CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
|
||||||
.setConnectionRequestTimeout(60000)
|
|
||||||
.build();
|
|
||||||
CloseableHttpClient client = HttpClients.custom()
|
|
||||||
.setDefaultRequestConfig(defaultRequestConfig).build();
|
|
||||||
|
|
||||||
// 创建并设置URI
|
// 创建并设置URI
|
||||||
URIBuilder uri = null;
|
URIBuilder uri = null;
|
||||||
@ -224,9 +132,7 @@ public class AddressUtils {
|
|||||||
}
|
}
|
||||||
entity = response.getEntity();
|
entity = response.getEntity();
|
||||||
is = entity.getContent();
|
is = entity.getContent();
|
||||||
br = new BufferedReader(
|
br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
|
||||||
new InputStreamReader(is, StandardCharsets.UTF_8)
|
|
||||||
);
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
while ((line = br.readLine()) != null) {
|
while ((line = br.readLine()) != null) {
|
||||||
sb.append(line);
|
sb.append(line);
|
||||||
@ -252,18 +158,18 @@ public class AddressUtils {
|
|||||||
return sLine;
|
return sLine;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void close(InputStream is, BufferedReader br, CloseableHttpResponse response, CloseableHttpClient client){
|
private static void close(InputStream is, BufferedReader br, CloseableHttpResponse response, CloseableHttpClient client) {
|
||||||
try {
|
try {
|
||||||
if (null != is){
|
if (null != is) {
|
||||||
is.close();
|
is.close();
|
||||||
}
|
}
|
||||||
if (null != br){
|
if (null != br) {
|
||||||
br.close();
|
br.close();
|
||||||
}
|
}
|
||||||
if (null != response){
|
if (null != response) {
|
||||||
response.close();
|
response.close();
|
||||||
}
|
}
|
||||||
if (null != client){
|
if (null != client) {
|
||||||
client.close();
|
client.close();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -272,18 +178,12 @@ public class AddressUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static HttpGet setGet(HttpGet get) {
|
private static HttpGet setGet(HttpGet get) {
|
||||||
get.setHeader("Accept"
|
get.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
|
||||||
, "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
|
get.setHeader("Accept-Encoding", "gzip, deflate");
|
||||||
get.setHeader("Accept-Encoding"
|
get.setHeader("Accept-Language", "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6");
|
||||||
, "gzip, deflate");
|
get.setHeader("Connection", "keep-alive");
|
||||||
get.setHeader("Accept-Language"
|
get.setHeader("Cache-Control", "no-cache");
|
||||||
, "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6");
|
get.setHeader("Upgrade-Insecure-Requests", "1");
|
||||||
get.setHeader("Connection"
|
|
||||||
, "keep-alive");
|
|
||||||
get.setHeader("Cache-Control"
|
|
||||||
, "no-cache");
|
|
||||||
get.setHeader("Upgrade-Insecure-Requests"
|
|
||||||
, "1");
|
|
||||||
get.setHeader("User-Agent", USER_AGENT);
|
get.setHeader("User-Agent", USER_AGENT);
|
||||||
return get;
|
return get;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user