在java项目Controller层加入下面代码:
public MapselectAccessory(HttpServletRequest request){
//public MapselectAccessory(HttpServletRequest request)根据自己需要改写
//下列代码可以直接复制
Mapmap=new HashMap();
System.out.println("取得客户端的系统版本 1="+request.getHeader("User-Agent")+
"取得客户端的IP 2="+request.getRemoteAddr()+
"取得客户端的主机名3="+request.getRemoteHost()+
"取得客户端的端口 4="+request.getRemotePort()+
"取得客户端的用户5="+request.getRemoteUser()+
"取得服务器IP6="+request.getLocalAddr()+
"取得服务器端口7="+request.getLocalPort());
//下面为获取客户端ip地址代码
String remoteAddr = request.getRemoteAddr();
String forwarded = request.getHeader("X-Forwarded-For");
String realIp = request.getHeader("X-Real-IP");
String ip = null;
if (realIp == null) {
if (forwarded == null) {
ip = remoteAddr;
} else {
ip = remoteAddr + "/" + forwarded.split(",")[0];
}
} else {
if (realIp.equals(forwarded)) {
ip = realIp;
} else {
if(forwarded != null){
forwarded = forwarded.split(",")[0];
}
ip = realIp + "/" + forwarded;
}
}
System.out.println("客户端ip="+ip);
//打印到控制台的都是你所需要的内容
return map;
}