package com.tongchuang.website.common.utils;
import com.alibaba.fastjson.JSONObject;
import com.tongchuang.website.common.constatnts.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 获取地址类
*
* @author
*/
public class AddressUtils
{
private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
// IP地址查询
public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";
// 未知地址
public static final String UNKNOWN = "XX XX";
public static String getRealAddressByIP(String ip)
{
String address = UNKNOWN;
// 内网不查询
if (IpUtils.internalIp(ip))
{
return "内网IP";
}
try
{
String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true", Constants.GBK);
if (StringUtils.isEmpty(rspStr))
{
log.error("获取地理位置异常 {}", ip);
return UNKNOWN;
}
JSONObject obj = JSONObject.parseObject(rspStr);
String region = obj.getString("pro");
String city = obj.getString("city");
return String.format("%s %s", region, city);
}
catch (Exception e)
{
log.error("获取地理位置异常 {}", ip);
}
return address;
}
//
public static String getV4IP() {
String ip=null;
try {
URL url = new URL("https://www.taobao.com/help/getip.php");
URLConnection URLconnection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) URLconnection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
System.out.println("成功");
InputStream in = httpConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader bufr = new BufferedReader(isr);
String str;
while ((str = bufr.readLine()) != null) {
ip=str;
System.out.println(str);
}
bufr.close();
} else {
System.err.println("失败");
}
} catch (Exception e) {
e.printStackTrace();
}
if(ip!=null){
char[] chs=ip.toCharArray();
ip="";
for(int i=0;i<chs.length;i++){
if((chs[i]>=48&&chs[i]<=57)||chs[i]==46){
ip+=chs[i];
}
}
}
return ip;
}
}
在我写完了 这个获取本地外网ip的方法后 发现了。。项目里面有一个的感想