JAVA后台获取客户端电脑名称,IP方法类
@ApiOperation(value = "获取电脑信息")
public ReturnResult<Map<String, Object>> getComputerInfo(HttpServletRequest request) throws Exception{
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("ip", getIpAddress(request));
InetAddress inetAddress = InetAddress.getByName(getIpAddress(request));
String computername = inetAddress.getHostName();
if ("localhost".equalsIgnoreCase(computername)) {
computername = InetAddress.getLocalHost().getCanonicalHostName();
}
String[] split = computername.split("\\.");
String name = split[0].toUpperCase();
dataMap.put("name",name);
return new ReturnResult<>("200", "OK", dataMap);
}
public static String getIpAddress(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
if (ip.contains(",")) {
return ip.split(",")[0];
} else {
return ip;
}
}