本文主要是介绍JavaWeb获取请求的ip地址,浏览器版本,操作系统工具类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1、引入依赖
<!--fastjson依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
<!--bitwalker依赖-->
<dependency>
<groupId>eu.bitwalker</groupId>
<artifactId>UserAgentUtils</artifactId>
<version>1.21</version>
</dependency>
2、获取数据
import com.alibaba.fastjson.JSONObject;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import eu.bitwalker.useragentutils.Version;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.InetAddress;
import java.net.UnknownHostException;
@RestController
public class TestController {
@GetMapping(value = {"/", "/index"})
public JSONObject index(HttpServletRequest request, HttpServletResponse response) {
/*获取请求IP*/
String requestIp = IpAndAddrUtil.getIp(request);
/*获取发起请求的浏览器版本号*/
String browserVersion = IpAndAddrUtil.getBrowserVersion(request);
/*获取发起请求的操作系统名称*/
String osName = IpAndAddrUtil.getOsName(request);
/*获取发起请求的浏览器名称*/
String browserName = IpAndAddrUtil.getBrowserName(request);
JSONObject jsonObject = new JSONObject();
jsonObject.put("ip", requestIp);
jsonObject.put("browserVersion", browserVersion);
jsonObject.put("osName", osName);
jsonObject.put("browserName", browserName);
return jsonObject;
}
}
/**
* 获取请求IP地址、浏览器相关信息
*/
class IpAndAddrUtil {
/**
* 获取发起请求的IP地址
*/
public static String getIp(HttpServletRequest request) {
String ip = null;
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.equals("127.0.0.1")) {
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ip = inet.getHostAddress();
}
}
if ((ip != null) && (ip.length() > 15)) {
if (ip.indexOf(",") > 0) {
ip = ip.substring(0, ip.indexOf(","));
}
}
return ip;
}
/**
* 获取发起请求的浏览器名称
*/
public static String getBrowserName(HttpServletRequest request) {
String header = request.getHeader("User-Agent");
UserAgent userAgent = UserAgent.parseUserAgentString(header);
Browser browser = userAgent.getBrowser();
return browser.getName();
}
/**
* 获取发起请求的浏览器版本号
*/
public static String getBrowserVersion(HttpServletRequest request) {
String header = request.getHeader("User-Agent");
UserAgent userAgent = UserAgent.parseUserAgentString(header);
//获取浏览器信息
Browser browser = userAgent.getBrowser();
//获取浏览器版本号
Version version = browser.getVersion(header);
return version.getVersion();
}
/**
* 获取发起请求的操作系统名称
*/
public static String getOsName(HttpServletRequest request) {
String header = request.getHeader("User-Agent");
UserAgent userAgent = UserAgent.parseUserAgentString(header);
OperatingSystem operatingSystem = userAgent.getOperatingSystem();
return operatingSystem.getName();
}
}
这篇关于JavaWeb获取请求的ip地址,浏览器版本,操作系统工具类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!