zoukankan      html  css  js  c++  java
  • RequestUtils 包装工具类

      1 package com.pc.core.util;
      2 
      3 import java.net.MalformedURLException;
      4 import java.net.URL;
      5 
      6 import javax.servlet.http.Cookie;
      7 import javax.servlet.http.HttpServletRequest;
      8 import javax.servlet.http.HttpServletResponse;
      9 
     10 import org.apache.commons.beanutils.ConvertUtils;
     11 import org.apache.commons.lang.StringUtils;
     12 import org.apache.commons.lang.math.NumberUtils;
     13 /**
     14 * RequestUtils 这个类主要是用来获取关于请求的一些信息的工具类 
     15 * @author YUNFENCGHENG
     16 * 2011-9-19 下午04:36:22
     17 */
     18 public class RequestUtils {
     19 
     20     /**
     21      * 获取客户端IP地址,此方法用在proxy环境中
     22      * @param req
     23      * @return
     24      */
     25     public static String getRemoteAddr(HttpServletRequest req) {
     26         String ip = req.getHeader("X-Forwarded-For");
     27         if(StringUtils.isNotBlank(ip)){
     28             String[] ips = StringUtils.split(ip,',');
     29             if(ips!=null){
     30                 for(String tmpip : ips){
     31                     if(StringUtils.isBlank(tmpip))
     32                         continue;
     33                     tmpip = tmpip.trim();
     34                     if(isIPAddr(tmpip) && !tmpip.startsWith("10.") && !tmpip.startsWith("192.168.") && !"127.0.0.1".equals(tmpip)){
     35                         return tmpip.trim();
     36                     }
     37                 }
     38             }
     39         }
     40         ip = req.getHeader("x-real-ip");
     41         if(isIPAddr(ip))
     42             return ip;
     43         ip = req.getRemoteAddr();
     44         if(ip.indexOf('.')==-1)
     45             ip = "127.0.0.1";
     46         return ip;
     47     }
     48 
     49     /**
     50      * 判断是否为搜索引擎
     51      * @param req
     52      * @return
     53      */
     54     public static boolean isRobot(HttpServletRequest req){
     55         String ua = req.getHeader("user-agent");
     56         if(StringUtils.isBlank(ua)) return false;
     57         return (ua != null
     58                 && (ua.indexOf("Baiduspider") != -1 || ua.indexOf("Googlebot") != -1
     59                         || ua.indexOf("sogou") != -1
     60                         || ua.indexOf("sina") != -1
     61                         || ua.indexOf("iaskspider") != -1
     62                         || ua.indexOf("ia_archiver") != -1
     63                         || ua.indexOf("Sosospider") != -1
     64                         || ua.indexOf("YoudaoBot") != -1
     65                         || ua.indexOf("yahoo") != -1 
     66                         || ua.indexOf("yodao") != -1
     67                         || ua.indexOf("MSNBot") != -1
     68                         || ua.indexOf("spider") != -1
     69                         || ua.indexOf("Twiceler") != -1
     70                         || ua.indexOf("Sosoimagespider") != -1
     71                         || ua.indexOf("naver.com/robots") != -1
     72                         || ua.indexOf("Nutch") != -1
     73                         || ua.indexOf("spider") != -1));    
     74     }
     75 
     76     /**
     77      * 获取COOKIE
     78      * 
     79      * @param name
     80      */
     81     public static Cookie getCookie(HttpServletRequest request, String name) {
     82         Cookie[] cookies = request.getCookies();
     83         if(cookies == null)    return null;
     84         for (Cookie ck : cookies) {
     85             if (StringUtils.equalsIgnoreCase(name,ck.getName())) 
     86                 return ck;            
     87         }
     88         return null;
     89     }
     90 
     91     /**
     92      * 获取COOKIE
     93      * 
     94      * @param name
     95      */
     96     public static String getCookieValue(HttpServletRequest request, String name) {
     97         Cookie[] cookies = request.getCookies();
     98         if(cookies == null)    return null;
     99         for (Cookie ck : cookies) {
    100             if (StringUtils.equalsIgnoreCase(name,ck.getName())) 
    101                 return ck.getValue();            
    102         }
    103         return null;
    104     }
    105 
    106     /**
    107      * 设置COOKIE
    108      * 
    109      * @param name
    110      * @param value
    111      * @param maxAge
    112      */
    113     public static void setCookie(HttpServletRequest request, HttpServletResponse response, String name,
    114             String value, int maxAge) {
    115         setCookie(request,response,name,value,maxAge,true);
    116     }
    117 
    118     /**
    119      * 设置COOKIE
    120      * 
    121      * @param name
    122      * @param value
    123      * @param maxAge
    124      */
    125     public static void setCookie(HttpServletRequest request, HttpServletResponse response, String name,
    126             String value, int maxAge, boolean all_sub_domain) {
    127         Cookie cookie = new Cookie(name, value);
    128         cookie.setMaxAge(maxAge);
    129         if(all_sub_domain){
    130             String serverName = request.getServerName();
    131             String domain = getDomainOfServerName(serverName);
    132             if(domain!=null && domain.indexOf('.')!=-1){
    133                 cookie.setDomain('.' + domain);
    134             }
    135         }
    136         cookie.setPath("/");
    137         response.addCookie(cookie);
    138     }
    139     
    140     public static void deleteCookie(HttpServletRequest request,
    141             HttpServletResponse response, String name, boolean all_sub_domain) {
    142         setCookie(request,response,name,"",0,all_sub_domain);
    143     }
    144 
    145     /**
    146      * 获取用户访问URL中的根域名
    147      * 例如: www.dlog.cn -> dlog.cn
    148      * @param req
    149      * @return
    150      */
    151     public static String getDomainOfServerName(String host){
    152         if(isIPAddr(host))
    153             return null;
    154         String[] names = StringUtils.split(host, '.');
    155         int len = names.length;
    156         if(len==1) return null;
    157         if(len==3){
    158             return makeup(names[len-2],names[len-1]);
    159         }
    160         if(len>3){
    161             String dp = names[len-2];
    162             if(dp.equalsIgnoreCase("com")||dp.equalsIgnoreCase("gov")||dp.equalsIgnoreCase("net")||dp.equalsIgnoreCase("edu")||dp.equalsIgnoreCase("org"))
    163                 return makeup(names[len-3],names[len-2],names[len-1]);
    164             else
    165                 return makeup(names[len-2],names[len-1]);
    166         }
    167         return host;
    168     }
    169 
    170     /**
    171      * 判断字符串是否是一个IP地址
    172      * @param addr
    173      * @return
    174      */
    175     public static boolean isIPAddr(String addr){
    176         if(StringUtils.isEmpty(addr))
    177             return false;
    178         String[] ips = StringUtils.split(addr, '.');
    179         if(ips.length != 4)
    180             return false;
    181         try{
    182             int ipa = Integer.parseInt(ips[0]);
    183             int ipb = Integer.parseInt(ips[1]);
    184             int ipc = Integer.parseInt(ips[2]);
    185             int ipd = Integer.parseInt(ips[3]);
    186             return ipa >= 0 && ipa <= 255 && ipb >= 0 && ipb <= 255 && ipc >= 0
    187                     && ipc <= 255 && ipd >= 0 && ipd <= 255;
    188         }catch(Exception e){}
    189         return false;
    190     }
    191     
    192     private static String makeup(String...ps){
    193         StringBuilder s = new StringBuilder();
    194         for(int idx = 0; idx < ps.length; idx++){
    195             if(idx > 0)
    196                 s.append('.');
    197             s.append(ps[idx]);
    198         }
    199         return s.toString();
    200     }
    201 
    202     /**
    203      * 获取HTTP端口
    204      * @param req
    205      * @return
    206      * @throws MalformedURLException
    207      */
    208     public static int getHttpPort(HttpServletRequest req) {
    209         try {
    210             return new URL(req.getRequestURL().toString()).getPort();
    211         } catch (MalformedURLException excp) {
    212             return 80;
    213         }
    214     }    
    215 
    216     /**
    217      * 获取浏览器提交的整形参数
    218      * @param param
    219      * @param defaultValue
    220      * @return
    221      */
    222     public static int getParam(HttpServletRequest req, String param, int defaultValue){
    223         return NumberUtils.toInt(req.getParameter(param), defaultValue);
    224     }
    225     /**
    226      * 获取浏览器提交的整形参数
    227      * @param param
    228      * @param defaultValue
    229      * @return
    230      */
    231     public static long getParam(HttpServletRequest req, String param, long defaultValue){
    232         return NumberUtils.toLong(req.getParameter(param), defaultValue);
    233     }
    234     
    235     public static long[] getParamValues(HttpServletRequest req, String name){
    236         String[] values = req.getParameterValues(name);
    237         if(values==null) return null;
    238         return (long[])ConvertUtils.convert(values, long.class);
    239     }
  • 相关阅读:
    ES6 Set
    JavaScript 之 对象属性的特性 和defineProperty方法
    ES6 ... 展开&收集运算符
    ES6 箭头函数
    ES6 解构 destructuring
    canvas之事件交互效果isPointPath
    跨域及JSONP原理
    P03 显示隐藏
    最长公共子序列
    P02 CSS样式
  • 原文地址:https://www.cnblogs.com/smile361/p/2855845.html
Copyright © 2011-2022 走看看