log日志记录

This commit is contained in:
2025-05-06 13:29:47 +08:00
parent 110e73b993
commit 5038cebbfa
11 changed files with 248 additions and 63 deletions

View File

@ -16,6 +16,7 @@ import org.springframework.web.context.request.RequestContextHolder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
@ -248,32 +249,32 @@ public class OperateLogAspect {
public String getIpLocation(String ip) {
try {
// 发送请求到ip-api服务
String url = "http://ip-api.com/json/" + ip + "?lang=zh-CN"; // 获取中文结果
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 解析JSON返回结果
JSONObject jsonResponse = JSONObject.parseObject(response.toString());
String region = jsonResponse.getString("regionName"); //
String city = jsonResponse.getString("city"); // 城市
String country = jsonResponse.getString("country"); // 国家
return city + ", " + region + ", " + country; // 返回位置
} catch (Exception e) {
e.printStackTrace();
}
// try {
// // 发送请求到ip-api服务
// String url = "http://ip-api.com/json/" + ip + "?lang=zh-CN"; // 获取中文结果
// URL obj = new URL(url);
// HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// con.setRequestMethod("GET");
//
// BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
// String inputLine;
// StringBuffer response = new StringBuffer();
//
// while ((inputLine = in.readLine()) != null) {
// response.append(inputLine);
// }
// in.close();
//
// // 解析JSON返回结果
// JSONObject jsonResponse = JSONObject.parseObject(response.toString());
// String country = jsonResponse.getString("country"); // 国家
// String region = jsonResponse.getString("regionName"); //
// String city = jsonResponse.getString("city"); // 城市
//
// return country + "-" + region + "-" + city; // 返回位置
// } catch (Exception e) {
// e.printStackTrace();
// }
return "未知";
}