java判断http请求是否为为手机端来源

java判断http请求是否为为手机端来源

package com.my.utils.http;

import javax.servlet.http.HttpServletRequest;

/**
 * 判断是否是手机请求
 * @author admin
 */
public class HttpResource {

    /**
     * android : 所有android设备 mac os : iphone ipad windows
     * phone:Nokia等windows系统的手机
     * @param request
     * @return
     */
    public static boolean isMobileDevice(HttpServletRequest request) {
        String requestHeader = request.getHeader("user-agent").toLowerCase();
        String[] deviceArray = new String[]{"android", "iphone", "ios", "windows phone"};
        if (requestHeader == null) {
            return false;
        }
        requestHeader = requestHeader.toLowerCase();
        for (int i = 0; i < deviceArray.length; i++) {
            if (requestHeader.indexOf(deviceArray[i]) > 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * android : 所有android设备 mac os : iphone ipad windows
     * phone:Nokia等windows系统的手机
     * @param requestHeader 请求头
     * @return
     */
    public static String getDevice(HttpServletRequest request) {
        String requestHeader = request.getHeader("user-agent").toLowerCase();
        String[] deviceArray = new String[]{"android", "iphone", "ios", "windows phone"};
        if (requestHeader == null) {
            return "PC端";
        }
        requestHeader = requestHeader.toLowerCase();
        for (int i = 0; i < deviceArray.length; i++) {
            if (requestHeader.indexOf(deviceArray[i]) > 0) {
                return "WAP端";
            }
        }
        return "PC端";
    }
    
    /**
     * 判断是否为微信
     * @param request
     * @return
     */
    public static boolean isWeChat(HttpServletRequest request){
        //判断 是否是微信浏览器
        String userAgent = request.getHeader("user-agent").toLowerCase();
        if(userAgent.indexOf("micromessenger")>-1){//微信客户端
            return true;
        }else{
            return false;
        }
    }
}

你可能感兴趣的:(java,web,springMVC)