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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package cn.chnmuseum.party.common.util;
import cn.hutool.core.net.NetUtil;
import net.sf.json.JSONObject;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class AddressUtil {
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();
}
// 多次反向代理后会有多个IP值,只有第一个是真实IP
if(null != ip && ip.length() > 15){
if(ip.indexOf(",") > 0){
ip = ip.substring(0, ip.indexOf(","));
}
}
return ip;
}
/**
* @param content 请求的参数 格式为:name=xxx&pwd=xxx
* @param encodingString 服务器端请求编码。如GBK,UTF-8等
* @return
* @throws UnsupportedEncodingException
*/
public static String getAddresses(String content, String encodingString) throws UnsupportedEncodingException {
//调用太平洋API
String urlStr = "http://whois.pconline.com.cn/ipJson.jsp?json=true";
String returnStr = getResult(urlStr, content, encodingString);
if (returnStr != null) {
System.out.println(returnStr);
return returnStr;
}
return null;
}
/**
* @param urlStr 请求的地址
* @param content 请求的参数 格式为:name=xxx&pwd=xxx
* @param encodingString 服务器端请求编码。如GBK,UTF-8等
* @return
*/
private static String getResult(String urlStr, String content, String encodingString) {
URL url = null;
HttpURLConnection connection = null;
try {
url = new URL(urlStr);
// 新建连接实例
connection = (HttpURLConnection) url.openConnection();
// 设置连接超时时间,单位毫秒
//connection.setConnectTimeout(20000);
// 设置读取数据超时时间,单位毫秒
//connection.setReadTimeout(20000);
//是否打开输出流
connection.setDoOutput(true);
//是否打开输入流
connection.setDoInput(true);
//提交方法 POST|GET
connection.setRequestMethod("POST");
//是否缓存
connection.setUseCaches(false);
//打开连接端口
connection.connect();
//打开输出流往对端服务器写数据
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
//写数据,即提交表单 name=xxx&pwd=xxx
out.writeBytes(content);
//刷新
out.flush();
//关闭输出流
out.close();
// 往对端写完数据对端服务器返回数据 ,以BufferedReader流来读取
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), encodingString));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return null;
}
public static Map<String, String> getAddressByIp(String ip) {
// json_result用于接收返回的json数据
String json_result = null;
Map<String, String> map = new HashMap<>();
try {
json_result = getAddresses("ip=" + ip, "gbk");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("json_result = " + json_result);
JSONObject json = JSONObject.fromObject(json_result);
System.out.println("json数据: " + json);
String pro = json.get("pro").toString();
String proCode = json.get("proCode").toString();
String city = json.get("city").toString();
String cityCode = json.get("cityCode").toString();
String region = json.get("region").toString();
String regionCode = json.get("regionCode").toString();
String addr = json.get("addr").toString();
// String country = json.get("country").toString();
// String isp = json.get("isp").toString();
// map.put("country", country);//国家
map.put("addr", addr);//区域
map.put("pro", pro);//省
map.put("proCode", proCode);//省编码
map.put("city", city);//市
map.put("cityCode", cityCode);//市编码
map.put("region", region);//区
map.put("regionCode", regionCode);//区编码
// map.put("isp", isp);//互联网服务提供商
return map;
}
public static void main(String[] args) {
Map<String, String> addressByIp1 = getAddressByIp("192.168.207.244");
System.out.println(addressByIp1);
}
}