首页 > 代码库 > 推断client手机类型,并跳转到对应的app下载页面

推断client手机类型,并跳转到对应的app下载页面

实现的原理,是检測浏览器的 USER-AGENT 这个header,然后依据正則表達式来确定client类型。 

假设都不匹配,Fallback回退策略是显示相应的页面。让用户自己选择。 
适合採用二维码扫描方式下载APP: 

JSP版本号的代码例如以下所看到的:其它服务端版本号请百度搜索。 

原帖地址: [您也能够訪问向上交流论坛查看很多其它行业,开源,招聘信息]

<%@page import="java.util.regex.Matcher"%>
<%@page import="java.util.regex.Pattern"%>
<%@ page language="java" pageEncoding="UTF-8"%>
<%!
// \b 是单词边界(连着的两个(字母字符 与 非字母字符) 之间的逻辑上的间隔),字符串在编译时会被转码一次,所以是 "\\b"
// \B 是单词内部逻辑间隔(连着的两个字母字符之间的逻辑上的间隔)

String androidReg = "\\bandroid|Nexus\\b";
String iosReg = "ip(hone|od|ad)";

Pattern androidPat = Pattern.compile(androidReg, Pattern.CASE_INSENSITIVE);
Pattern iosPat = Pattern.compile(iosReg, Pattern.CASE_INSENSITIVE);

public boolean likeAndroid(String userAgent){
	if(null == userAgent){
		userAgent = "";
	}
	// 匹配
	Matcher matcherAndroid = androidPat.matcher(userAgent);
	if(matcherAndroid.find()){
		return true;
	} else {
		return false;
	}
}
public boolean likeIOS(String userAgent){
	if(null == userAgent){
		userAgent = "";
	}
	// 匹配
	Matcher matcherIOS = iosPat.matcher(userAgent);
	if(matcherIOS.find()){
		return true;
	} else {
		return false;
	}
}

%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

//
String userAgent = request.getHeader( "USER-AGENT" ).toLowerCase();
System.out.println("userAgent: "+userAgent);
if(null == userAgent){
	userAgent = "";
}
if(likeAndroid(userAgent)){
System.out.println("likeAndroid: "+true);
	response.sendRedirect("http://m.iyhjy.com/download.jsp?platform=android");
	return;
	//request.getRequestDispatcher("/download.html").forward(request,response);
} else if(likeIOS(userAgent)){
System.out.println("likeIOS: "+true);
	response.sendRedirect("http://itunes.apple.com/us/app/id714751061");
	return;
	//request.getRequestDispatcher("/index.html").forward(request,response);
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<title>下载client - 永恒记忆</title>
<link href="http://www.mamicode.com/css/style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div class="p_down">
	<div>
		<a href="http://www.mamicode.com/index.html">
		<img src="http://www.mamicode.com/images/p_logo.png" class="p_logo" />
		</a>
	</div> 
		
		<a href="http://www.mamicode.com/itms-services://?

action=download-manifest&url=http://m.iyhjy.com/upload/client/yhjyios.plist" class="apple download"><img src="http://www.mamicode.com/images/p_down_apple.png" /></a> <a href="http://m.iyhjy.com/download.jsp?platform=android" class="download"><img src="http://www.mamicode.com/images/p_down_and.png" /></a> </div> </body> </html>



推断client手机类型,并跳转到对应的app下载页面