优化一些细节

This commit is contained in:
RuoYi
2018-07-12 09:58:12 +08:00
parent b8b49209ca
commit df12e06a3d
4 changed files with 264 additions and 63 deletions

View File

@ -1,13 +1,7 @@
package com.ruoyi.common.utils;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import com.ruoyi.common.utils.http.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -22,65 +16,12 @@ public class AddressUtils
public static final String IP_URL = "http://ip.taobao.com/service/getIpInfo.php";
/**
* 获取查询结果
*
* @param urlStr
* @param content
* @param encoding
* @return
*/
private static String sendPost(String content, String encoding)
{
URL url = null;
HttpURLConnection connection = null;
try
{
url = new URL(IP_URL);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(2000);
connection.setReadTimeout(2000);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.connect();
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(content);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), encoding));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null)
{
buffer.append(line);
}
reader.close();
return buffer.toString();
}
catch (IOException e)
{
log.error("温馨提醒:您的主机已经断网,请您检查主机的网络连接");
log.error("根据IP获取所在位置----------错误消息:" + e.getMessage());
}
finally
{
if (connection != null)
{
connection.disconnect();
}
}
return null;
}
public static String getRealAddressByIP(String ip)
{
String address = "";
try
{
address = sendPost("ip=" + ip, Constants.UTF8);
address = HttpUtils.sendPost(IP_URL, "ip=" + ip);
JSONObject json = JSONObject.parseObject(address);
JSONObject object = json.getObject("data", JSONObject.class);
String region = object.getString("region");