java获取IP地址、操作系统类型和手机型号

package com.suobei.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by wangmin on 2018/01/21.
 */
@WebServlet("/user")
public class UserServlet extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String referrer = request.getHeader("Referer");
        System.out.println(referrer);
        String remoteAddr = request.getRemoteAddr();
        if(remoteAddr.equals("0:0:0:0:0:0:0:1")){
            System.out.println("您的ip地址为:127.0.0.1");
        }else{
            System.out.println("您的ip地址为:" + remoteAddr);
        }
        String requestHeader = request.getHeader("User-Agent");
        int index_one = requestHeader.indexOf("(");
        String requestBody = requestHeader.substring(index_one+1);
        String userInfo = requestBody.substring(0, requestBody.indexOf(")"));
        String[] userInfoList = userInfo.split(";");
        int length = userInfoList.length;
        String os = userInfoList[0];
        String mobileInfo = userInfoList[length - 1];
        if(os.equals("Windows NT 6.1")){
            System.out.println("您的操作系统为:windows7");
        }else{
            System.out.println("您的操作系统为:" + os);
        }
        int index = mobileInfo.indexOf("/");
        if(index > 0){
            mobileInfo = mobileInfo.substring(0, mobileInfo.indexOf("/") - 5);
            System.out.println("您的手机型号为:" + mobileInfo);
        }

    }
}
 
 

你可能感兴趣的:(服务器问题)