From 4f1f0bdbd306e06c88e99408fb9a5a456f38162c Mon Sep 17 00:00:00 2001 From: RuoYi Date: Thu, 14 May 2020 14:27:47 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E7=94=A8http=E5=8F=91=E9=80=81?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=A2=9E=E5=8A=A0=E5=8F=82=E6=95=B0=20conten?= =?UTF-8?q?tType=20=E7=BC=96=E7=A0=81=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/common/constant/Constants.java | 7 ++++++- .../com/ruoyi/common/utils/http/HttpUtils.java | 16 +++++++++++++++- .../com/ruoyi/common/utils/ip/AddressUtils.java | 3 ++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/ruoyi/src/main/java/com/ruoyi/common/constant/Constants.java b/ruoyi/src/main/java/com/ruoyi/common/constant/Constants.java index bde0d4a..63ea9ed 100644 --- a/ruoyi/src/main/java/com/ruoyi/common/constant/Constants.java +++ b/ruoyi/src/main/java/com/ruoyi/common/constant/Constants.java @@ -13,7 +13,12 @@ public class Constants * UTF-8 字符集 */ public static final String UTF8 = "UTF-8"; - + + /** + * GBK 字符集 + */ + public static final String GBK = "GBK"; + /** * 通用成功标识 */ diff --git a/ruoyi/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java b/ruoyi/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java index 4700436..2078240 100644 --- a/ruoyi/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java +++ b/ruoyi/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java @@ -18,6 +18,7 @@ import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.ruoyi.common.constant.Constants; /** * 通用http发送方法 @@ -36,6 +37,19 @@ public class HttpUtils * @return 所代表远程资源的响应结果 */ public static String sendGet(String url, String param) + { + return sendGet(url, param, Constants.UTF8); + } + + /** + * 向指定 URL 发送GET方法的请求 + * + * @param url 发送请求的 URL + * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 + * @param contentType 编码类型 + * @return 所代表远程资源的响应结果 + */ + public static String sendGet(String url, String param, String contentType) { StringBuilder result = new StringBuilder(); BufferedReader in = null; @@ -49,7 +63,7 @@ public class HttpUtils connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); connection.connect(); - in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK")); + in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType)); String line; while ((line = in.readLine()) != null) { diff --git a/ruoyi/src/main/java/com/ruoyi/common/utils/ip/AddressUtils.java b/ruoyi/src/main/java/com/ruoyi/common/utils/ip/AddressUtils.java index 8402a0d..c36431f 100644 --- a/ruoyi/src/main/java/com/ruoyi/common/utils/ip/AddressUtils.java +++ b/ruoyi/src/main/java/com/ruoyi/common/utils/ip/AddressUtils.java @@ -3,6 +3,7 @@ package com.ruoyi.common.utils.ip; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.alibaba.fastjson.JSONObject; +import com.ruoyi.common.constant.Constants; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.http.HttpUtils; import com.ruoyi.framework.config.RuoYiConfig; @@ -34,7 +35,7 @@ public class AddressUtils { try { - String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true"); + String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true", Constants.GBK); if (StringUtils.isEmpty(rspStr)) { log.error("获取地理位置异常 {}", ip);