通过该代码获取用户IP
/**
* 获取用户IP地址
* @param request
* @return
*/
public static String getIp(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_X_FORWARDED_FOR");
}
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.getRemoteAddr();
}
if (ip.equals("0:0:0:0:0:0:0:1")) {
ip = "localhost";
}
return ip;
}
通过IP获取用户地址
第一种方式 通过 ip2region.db
下载地址链接:https://pan.baidu.com/s/1bxcV3rqddON1lxTvC7pqbw
提取码:0066
文件db放到该路径
代码部分
/**
* 根据ip获取详细地址
*/
public static String getCityInfo(String ip) {
DbSearcher searcher = null;
try {
String path = "ip2region/ip2region.db";
String name = "ip2region.db";
DbConfig config = new DbConfig();
File file = inputStreamToFile(new ClassPathResource(path).getStream(), name);
searcher = new DbSearcher(config, file.getPath());
Method method;
method = searcher.getClass().getMethod("btreeSearch", String.class);
DataBlock dataBlock;
dataBlock = (DataBlock) method.invoke(searcher, ip);
String address = dataBlock.getRegion().replace("0|", "");
char symbol = '|';
if (address.charAt(address.length() - 1) == symbol) {
address = address.substring(0, address.length() - 1);
}
return address.equals("内网IP|内网IP") ? "内网IP" : address;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (searcher != null) {
try {
searcher.close();
} catch (IOException ignored) {
}
}
}
return "";
}
/**
* inputStream 转 File
*/
public static File inputStreamToFile(InputStream ins, String name) throws Exception {
File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);
if (file.exists()) {
return file;
}
OutputStream os = new FileOutputStream(file);
int bytesRead;
int len = 8192;
byte[] buffer = new byte[len];
while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
return file;
}
运行结果
第二种方式 通过 http://ip-api.com/json
代码部分
private static final OkHttpClient client = new OkHttpClient().newBuilder()
.build();
/**
* 获取地区信息
* @param ip
* @return
* @throws IOException
*/
public static IpAreaInfo getRegionalIn(String ip) throws IOException {
String api = "http://ip-api.com/json/"+ip+"?lang=zh-CN";
Request request = new Request.Builder()
.url(api)
.method("GET", null)
.build();
Response response = client.newCall(request).execute();
String result = response.body().string();
System.out.println(result);
JSONObject jsonObject = JSONObject.parseObject(result);
IpAreaInfo ipAreaInfo = JsonUtil.fromJson(jsonObject.toString(), IpAreaInfo.class);
System.out.println(ipAreaInfo);
return ipAreaInfo;
}
运行结果
以上两种方式 都是免费的