根据USER-AGENT判断手机类型并跳转到相应的app下载页面
实现的原理,是检测浏览器的USER-AGENT这个header,然后根据正则表达式来确定客户端类型。
如果都不匹配,Fallback回退策略是显示对应的页面,让用户自己选择。
适合采用二维码扫描方式下载APP:
JSP版本的代码如下所示:其他服务端版本请百度搜索。
<%@pageimport="java.util.regex.Matcher"%> <%@pageimport="java.util.regex.Pattern"%> <%@pagelanguage="java"pageEncoding="UTF-8"%> <%! //\b是单词边界(连着的两个(字母字符与非字母字符)之间的逻辑上的间隔),字符串在编译时会被转码一次,所以是"\\b" //\B是单词内部逻辑间隔(连着的两个字母字符之间的逻辑上的间隔) StringandroidReg="\\bandroid|Nexus\\b"; StringiosReg="ip(hone|od|ad)"; PatternandroidPat=Pattern.compile(androidReg,Pattern.CASE_INSENSITIVE); PatterniosPat=Pattern.compile(iosReg,Pattern.CASE_INSENSITIVE); publicbooleanlikeAndroid(StringuserAgent){ if(null==userAgent){ userAgent=""; } //匹配 MatchermatcherAndroid=androidPat.matcher(userAgent); if(matcherAndroid.find()){ returntrue; }else{ returnfalse; } } publicbooleanlikeIOS(StringuserAgent){ if(null==userAgent){ userAgent=""; } //匹配 MatchermatcherIOS=iosPat.matcher(userAgent); if(matcherIOS.find()){ returntrue; }else{ returnfalse; } } %> <% Stringpath=request.getContextPath(); StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; // StringuserAgent=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); }elseif(likeIOS(userAgent)){ System.out.println("likeIOS:"+true); response.sendRedirect("http://itunes.apple.com/us/app/id714751061"); return; //request.getRequestDispatcher("/index.html").forward(request,response); } %> <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> <metaname="viewport"content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no"> <title>下载客户端-永恒记忆</title> <linkhref="css/style.css"rel="stylesheet"type="text/css"/> </head> <body> <divclass="p_down"> <div> <ahref="index.html"> <imgsrc="images/p_logo.png"class="p_logo"/> </a> </div> <ahref="itms-services://?action=download-manifest&url=http://m.iyhjy.com/upload/client/yhjyios.plist"class="appledownload"><imgsrc="images/p_down_apple.png"/></a> <ahref="http://m.iyhjy.com/download.jsp?platform=android"class="download"><imgsrc="images/p_down_and.png"/></a> </div> </body> </html>