From 1b2c5d5c8ba022a68e23f08bab48198529de1fca Mon Sep 17 00:00:00 2001
From: 罗元桥 <2376770955@qq.com>
Date: 星期一, 27 九月 2021 16:56:42 +0800
Subject: [PATCH] Merge branch 'test_bak' into 'master'
---
springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/IPUtil.java | 63 +++++++++++++++----------------
1 files changed, 31 insertions(+), 32 deletions(-)
diff --git a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/IPUtil.java b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/IPUtil.java
index 52757e0..0b25a1a 100644
--- a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/IPUtil.java
+++ b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/IPUtil.java
@@ -6,15 +6,11 @@
import java.net.HttpURLConnection;
import java.net.URL;
-
import javax.servlet.http.HttpServletRequest;
-
import com.alibaba.fastjson.JSONObject;
+
import lombok.extern.slf4j.Slf4j;
-
-
-
/**
* @ClassName: IPUtil
@@ -28,92 +24,94 @@
@Slf4j
public class IPUtil {
-
-
/**
*
* 描述:获取IP地址
+ *
* @author huaping hu
* @date 2016年6月1日下午5:25:44
* @param request
* @return
*/
- public static String getIpAddress(HttpServletRequest request){
+ public static String getIpAddress(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
- if(ip == null || ip.length() ==0 || "nuknown".equalsIgnoreCase(ip)){
+ if (ip == null || ip.length() == 0 || "nuknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
- if(ip == null || ip.length() ==0 || "nuknown".equalsIgnoreCase(ip)){
+ if (ip == null || ip.length() == 0 || "nuknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
- if(ip == null || ip.length() ==0 || "nuknown".equalsIgnoreCase(ip)){
+ if (ip == null || ip.length() == 0 || "nuknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
-
/**
*
* 描述:获取IP+[IP所属地址]
+ *
* @author huaping hu
* @date 2016年6月1日下午6:01:09
* @param request
* @return
*/
- public static String getIpBelongAddress(HttpServletRequest request){
+ public static String getIpBelongAddress(HttpServletRequest request) {
String ip = getIpAddress(request);
String belongIp = getIPbelongAddress(ip);
return ip + belongIp;
}
+
/**
*
* 描述:获取IP所属地址
+ *
* @author huaping hu
* @date 2016年6月1日下午5:59:43
* @param ip
* @return
*/
- public static String getIPbelongAddress(String ip){
+ public static String getIPbelongAddress(String ip) {
String ipAddress = "[]";
- try{
-//淘宝提供的服务地址
- String context = call("http://ip.taobao.com/service/getIpInfo.php?ip="+ip);
+ try {
+ // 淘宝提供的服务地址
+ String context = call("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
JSONObject fromObject = JSONObject.parseObject(context);
String code = fromObject.getString("code");
- if(code.equals("0")){
+ if (code.equals("0")) {
JSONObject jsonObject = fromObject.getJSONObject("data");
- ipAddress = "["+jsonObject.get("country")+"/" +jsonObject.get("city")+"]";
+ ipAddress = "[" + jsonObject.get("country") + "/" + jsonObject.get("city") + "]";
}
- }catch(Exception e){
-
- log.error("获取IP所属地址出错",e);
- e.printStackTrace();
+ } catch (Exception e) {
+ log.error("获取IP所属地址出错");
+ // e.printStackTrace();
}
return ipAddress;
}
+
/**
*
* 描述:获取Ip所属地址
+ *
* @author huaping hu
* @date 2016年6月1日下午5:38:55
* @param urlStr
* @return
*/
- public static String call( String urlStr ){
+ public static String call(String urlStr) {
try {
URL url = new URL(urlStr);
- HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
+ HttpURLConnection httpCon = (HttpURLConnection)url.openConnection();
httpCon.setConnectTimeout(3000);
httpCon.setDoInput(true);
@@ -121,11 +119,11 @@
int code = httpCon.getResponseCode();
- if(code == 200){
+ if (code == 200) {
return streamConvertToSting(httpCon.getInputStream());
}
} catch (Exception e) {
- log.error("获取IP所属地址出错",e);
+ log.error("获取IP所属地址出错", e);
e.printStackTrace();
}
return null;
@@ -134,22 +132,24 @@
/**
*
* 描述:将InputStream转换成String
+ *
* @author huaping hu
* @date 2016年6月1日下午5:51:53
* @param is
* @return
*/
- public static String streamConvertToSting(InputStream is){
+ public static String streamConvertToSting(InputStream is) {
String tempStr = "";
try {
- if(is == null) return null;
+ if (is == null)
+ return null;
ByteArrayOutputStream arrayOut = new ByteArrayOutputStream();
byte[] by = new byte[1024];
int len = 0;
- while((len = is.read(by))!=-1){
- arrayOut.write(by,0,len);
+ while ((len = is.read(by)) != -1) {
+ arrayOut.write(by, 0, len);
}
tempStr = new String(arrayOut.toByteArray());
@@ -160,7 +160,6 @@
}
public static void main(String[] args) {
-
String context = call("http://ip.taobao.com/service/getIpInfo.php?ip=120.192.182.1");
--
Gitblit v1.7.1