利用AddressUtil 获取ip
public static String getIpAddr(HttpServletRequest request) {
String ipAddress = null;
//ipAddress = this.getRequest().getRemoteAddr();
ipAddress = request.getHeader("x-forwarded-for");
if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if(ipAddress.equals("127.0.0.1")){
//根据网卡取本机配置的IP
InetAddress inet=null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress= inet.getHostAddress();
}
}
//对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15
if(ipAddress.indexOf(",")>0){
ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));
}
}
return ipAddress;
}
```
通过请求调用
```
@RequestMapping(value="/ip", method = {RequestMethod.POST, RequestMethod.GET})
public @ResponseBody JSONResult ip(HttpServletRequest request, HttpServletResponse response) {
JSONResult jsonResult = new JSONResult();
// 定位
try {
jsonResult.setData(AddressUtils.getIpAddr(request));
jsonResult.setStatusCode(Constants.STAUTS_CODE_SUCCESS);
} catch (Exception e) {
jsonResult.setStatusCode(Constants.STAUTS_CODE_ERROR);
e.printStackTrace();
}
return jsonResult;
}
```
```
public static String getAddressByIP(String strIP) {
String address = "";
if(StringUtils.isEmpty(strIP)){
return address;
}
if(!isIpv4(strIP)){
return address;
}
try {
URL url = new URL( "http://ip.qq.com/cgi-bin/searchip?searchip1=" + strIP);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "GBK"));
String line = null;
StringBuffer result = new StringBuffer();
while((line = reader.readLine()) != null)
{
result.append(line);
}
reader.close();
strIP = result.substring(result.indexOf( "该IP所在地为:" ));
strIP = strIP.substring(strIP.indexOf( ":") + 1);
String province = strIP.substring(6, strIP.indexOf("省"));
String city = strIP.substring(strIP.indexOf("省") + 1, strIP.indexOf("市"));
address = province + "#" + city;
} catch ( IOException e) {
}
return address;
}
```
```
// 调用百度api ip get address
public static String getAddressByIP2(String strIP) {
String address = "";
if(StringUtils.isEmpty(strIP)){
return address;
}
if(!isIpv4(strIP)){
return address;
}
try {
// 当前毫秒数
String time_in_millis = Calendar.getInstance().getTimeInMillis() + "";
// jsonp callback 函数
String callback = "wotehao_" + time_in_millis;
// url
String href = "http://opendata.baidu.com/api.php?query={0}&resource_id={1}&t={2}&ie={3}&oe={4}&format={5}&tn={6}&cb={7}&_={8}";
// 封装参数
href = MessageFormat.format(href, strIP, "6006", time_in_millis, "utf8", "gbk", "json", "baidu", callback, time_in_millis);
//System.out.println("======" + callback);
URL url = new URL(href);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "GBK"));
String line = null;
StringBuffer result = new StringBuffer();
while((line = reader.readLine()) != null)
{
result.append(line);
}
String str_callback = result.toString();
//System.out.println("======" + str_callback);
str_callback = str_callback.replace("/**/"+callback+"(", "").replace(");", "");
JsonObject jsonObj_callback = new JsonParser().parse(str_callback).getAsJsonObject();
JsonObject jsonObj_data = jsonObj_callback.get("data").getAsJsonArray().get(0).getAsJsonObject();
address = jsonObj_data.get("location").getAsString();
} catch ( IOException e) {
}
return address;
}
```
判断是否为IPV4
```
public static boolean isIpv4(String ipAddress) {
String ip = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
+"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
Pattern pattern = Pattern.compile(ip);
Matcher matcher = pattern.matcher(ipAddress);
return matcher.matches();
}
```