首页 > 代码库 > 【java】服务器端获取用户访问的URL/用户IP/PC还是移动端

【java】服务器端获取用户访问的URL/用户IP/PC还是移动端

    @RequestMapping(value="http://www.mamicode.com/test")
    @ResponseBody
    public void test1(HttpServletRequest request,Questionnaire quest,String  questOptions){
        String ipAddress = null;
         if (request.getHeader("x-forwarded-for") == null) {  
             ipAddress = request.getRemoteAddr();
         }else{
            if(request.getHeader("x-forwarded-for").length()  > 15){
                String [] aStr = request.getHeader("x-forwarded-for").split(",");
                ipAddress = aStr[0];
            } else{
                ipAddress = request.getHeader("x-forwarded-for");
            }
         }  
         
         String terminal = request.getHeader("User-Agent");
        if(terminal.contains("Windows NT")){
            terminal = "PC端";
        }else{
            terminal = "移动端";
        }
        System.out.println("访客IP:"+ipAddress);
        System.out.println("用户访问来源:"+terminal);
        System.out.println("访问URL:"+request.getRequestURL());
    }

 

【java】服务器端获取用户访问的URL/用户IP/PC还是移动端