From 04f09e52ffd4681bdfd85e51acd3da0d1280c3d3 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期六, 16 四月 2022 16:07:10 +0800 Subject: [PATCH] bug修复 --- src/main/java/com/yeshi/buwan/util/IPUtil.java | 109 +++++++++++++++++++++++++++++++++--------------------- 1 files changed, 66 insertions(+), 43 deletions(-) diff --git a/src/main/java/com/yeshi/buwan/util/IPUtil.java b/src/main/java/com/yeshi/buwan/util/IPUtil.java index cd8c277..02a1879 100644 --- a/src/main/java/com/yeshi/buwan/util/IPUtil.java +++ b/src/main/java/com/yeshi/buwan/util/IPUtil.java @@ -1,22 +1,18 @@ package com.yeshi.buwan.util; +import com.show.api.ShowApiRequest; +import net.sf.json.JSONObject; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpException; +import org.apache.commons.httpclient.methods.GetMethod; + +import javax.servlet.http.HttpServletRequest; import java.io.BufferedInputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; -import java.util.HashMap; -import java.util.Map; - -import javax.servlet.http.HttpServletRequest; - -import com.show.api.ShowApiRequest; -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.HttpException; -import org.apache.commons.httpclient.methods.GetMethod; - -import net.sf.json.JSONObject; public class IPUtil { // http://verx.daili666.com/ip/?tid=558287898012218&num=1&operator=2 @@ -261,42 +257,28 @@ return ip; } - public static Map<String, String> getIPInfo(String ip) { - Map<String, String> map = new HashMap<String, String>(); - HttpClient client = new HttpClient(); - GetMethod method = new GetMethod("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip); - try { - client.executeMethod(method); - String result = method.getResponseBodyAsString(); - JSONObject object = JSONObject.fromObject(result); - if (object.optInt("code") == 0) { - map.put("city", object.optJSONObject("data").optString("city")); - map.put("country", object.optJSONObject("data").optString("country")); - return map; - } - } catch (HttpException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return map; + /** + * 鑾峰彇杩滅▼绔彛 + * + * @param request + * @return + */ + public static int getRemotePort(HttpServletRequest request) { + return request.getRemotePort(); } public static String getIPContry(String ip) { - String res = new ShowApiRequest("http://route.showapi.com/2152-1", "49053", "983e020ce60042cd95f39b3ebd4b9563") - .addTextPara("ip", ip) - .post(); - JSONObject json = JSONObject.fromObject(res); - if (json.optInt("showapi_res_code") == 0) { - JSONObject body = json.optJSONObject("showapi_res_body"); - if (body != null) - return body.optString("country"); - } - return ""; + IPInfo ipInfo = getIPInfo(ip); + return ipInfo == null ? "" : ipInfo.getCountry(); } public static String getIPProvince(String ip) { + IPInfo ipInfo = getIPInfo(ip); + return ipInfo == null ? "" : ipInfo.getProvince(); + } + + public static IPInfo getIPInfo(String ip) { try { String res = new ShowApiRequest("http://route.showapi.com/2152-1", "49053", "983e020ce60042cd95f39b3ebd4b9563") .addTextPara("ip", ip) @@ -304,13 +286,54 @@ JSONObject json = JSONObject.fromObject(res); if (json.optInt("showapi_res_code") == 0) { JSONObject body = json.optJSONObject("showapi_res_body"); - if (body != null) - return body.optString("state"); + if (body != null) { + String country = body.optString("country"); + String state = body.optString("state"); + String city = body.optString("city"); + return new IPInfo(country, state, city); + } } } catch (Exception e) { } - return ""; + return null; + } + + + public static class IPInfo { + private String province; + private String city; + private String country; + + public IPInfo(String country, String province, String city) { + this.province = province; + this.city = city; + this.country = country; + } + + public String getProvince() { + return province; + } + + public void setProvince(String province) { + this.province = province; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } } } -- Gitblit v1.8.0