CusAccessObjectUtil.java
3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package org.theyeasy.weixin.util;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.activemq.console.Main;
import com.alibaba.fastjson.JSONObject;
import com.w1hd.zzhnc.util.CommonUtil;
import com.w1hd.zzhnc.util.JsonMapper;
public class CusAccessObjectUtil {
public static String getIpAddress(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
public static String getIpCity(HttpServletRequest request) {
String sendGet = CommonUtil.sendGet("http://ip.taobao.com/service/getIpInfo.php?ip=", getIpAddress(request));
JSONObject json = JSONObject.parseObject(sendGet);
if (json.containsKey("city")) {
return json.get("city").toString();
}
return null;
}
public static String getCityName(double lng, double lat) {
JSONObject jsonObject = locationResult(lng, lat);
JSONObject jsonObject2 = jsonObject.getJSONObject("addressComponent");
String string = jsonObject2.getString("city");
System.out.println(string);
return string;
}
public static JSONObject locationResult(double lng, double lat) {
if (lng == 0 || lat == 0)
return null;
String result = WxMiniUtil.getAddress(lat + "", lng + "");
// String s =
// CommonUtil.sendGet("http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location="
// + lat
// + "," + lng + "&output=json&pois=1&ak=j6opW8N1iVvrkOPtEYNGMNH5nu3GbBhT",
// null);
// String substring = s.substring(0, s.length() - 1);
// System.out.println(s);
// if (substring.contains("限制访问")) {
// s =
// CommonUtil.sendGet("http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location="
// + lat + ","
// + lng + "&output=json&pois=1&ak=QjspiUqEwnqdEvsVsdse9yGDj8QoCCub", null);
// substring = s.substring(0, s.length() - 1);
// }
// String replace = substring.replace("renderReverse&&renderReverse(", "");
JSONObject parse = (JSONObject) JSONObject.parse(result);
if (parse.getInteger("status") == 302) {
}
JSONObject jsonObject = parse.getJSONObject("result");
return jsonObject;
}
public static String getProvince(double lng, double lat) {
JSONObject jsonObject = locationResult(lng, lat);
// JSONObject jsonObject2 = jsonObject.getJSONObject("result");
String string = jsonObject.getString("province");
return string;
}
public static String getProvince(JSONObject jsonObject) {
if (jsonObject == null) {
return null;
}
String string = jsonObject.getString("province");
return string;
}
public static String getCity(JSONObject jsonObject) {
if (jsonObject == null) {
return null;
}
String string = jsonObject.getString("city");
return string;
}
public static String getDistrict(JSONObject jsonObject) {
if (jsonObject == null) {
return null;
}
String string = jsonObject.getString("district");
return string;
}
public static String getFormattedAddress(JSONObject jsonObject) {
if (jsonObject == null) {
return null;
}
return jsonObject.getString("address");
}
public static void main(String[] arg) {
JSONObject json = CusAccessObjectUtil2.locationResult(114.061501, 22.542600);
System.out.println(CusAccessObjectUtil2.getFormattedAddress(json));
System.out.println(CusAccessObjectUtil2.getProvince(json));
System.out.println(CusAccessObjectUtil2.getCity(json));
System.out.println(CusAccessObjectUtil2.getDistrict(json));
}
}