terminalUtils 发表于 2017-04-23 终端识别工具类 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263package beldon.util;import javax.servlet.http.HttpServletRequest;import java.util.regex.Pattern;/** * 终端识别处理类 * Created by Beldon on. * Copyright (c) 2017-03-29, All Rights Reserved. */public abstract class TerminalUtils { static String phoneReg = "\\b(ip(hone|od)|android|opera m(ob|in)i" + "|windows (phone|ce)|blackberry" + "|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp" + "|laystation portable)|nokia|fennec|htc[-_]" + "|mobile|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\\b"; static String tableReg = "\\b(ipad|tablet|(Nexus 7)|up.browser" + "|[1-4][0-9]{2}x[1-4][0-9]{2})\\b"; //移动设备正则匹配:手机端、平板 static Pattern phonePat = Pattern.compile(phoneReg, Pattern.CASE_INSENSITIVE); static Pattern tablePat = Pattern.compile(tableReg, Pattern.CASE_INSENSITIVE); /** * 检测是否是移动设备访问 * * @param userAgent 浏览器标识 * @return true:移动设备接入,false:pc端接入 */ public static boolean isMobile(String userAgent) { return StringUtils.hasText(userAgent) && (phonePat.matcher(userAgent).find() || tablePat.matcher(userAgent).find()); } /** * 检测是否是移动设备访问 * * @param request HttpServletRequest * @return */ public static boolean isMobile(HttpServletRequest request) { return request != null && isMobile(request.getHeader("user-agent")); } /** * 判断是否是微信浏览器 * * @param userAgent 浏览器标识 * @return true:微信端,false:移动端 */ public static boolean isWechat(String userAgent) { return StringUtils.hasText(userAgent) && userAgent.contains("MicroMessenger"); } /** * 判断是否是微信浏览器 * * @param request HttpServletRequest * @return */ public static boolean isWechat(HttpServletRequest request) { return request != null && isWechat(request.getHeader("user-agent")); }}