• 实用工具类(持续更新)


    Java脱敏工具

     1 private static final int SIZE = 6;
     2     private static final String SYMBOL = "*";
     3     /**
     4      * 通用脱敏方法
     5      * @param value
     6      * @return
     7      */
     8     public static String commonDisplay(String value) {
     9         if (null == value || "".equals(value)) {
    10             return value;
    11         }
    12         int len = value.length();
    13         int pamaone = len / 2;
    14         int pamatwo = pamaone - 1;
    15         int pamathree = len % 2;
    16         StringBuilder stringBuilder = new StringBuilder();
    17         if (len <= 2) {
    18             if (pamathree == 1) {
    19                 return SYMBOL;
    20             }
    21             stringBuilder.append(SYMBOL);
    22             stringBuilder.append(value.charAt(len - 1));
    23         } else {
    24             if (pamatwo <= 0) {
    25                 stringBuilder.append(value.substring(0, 1));
    26                 stringBuilder.append(SYMBOL);
    27                 stringBuilder.append(value.substring(len - 1, len));
    28 
    29             } else if (pamatwo >= SIZE / 2 && SIZE + 1 != len) {
    30                 int pamafive = (len - SIZE) / 2;
    31                 stringBuilder.append(value.substring(0, pamafive));
    32                 for (int i = 0; i < SIZE; i++) {
    33                     stringBuilder.append(SYMBOL);
    34                 }
    35                 if ((pamathree == 0 && SIZE / 2 == 0) || (pamathree != 0 && SIZE % 2 != 0)) {
    36                     stringBuilder.append(value.substring(len - pamafive, len));
    37                 } else {
    38                     stringBuilder.append(value.substring(len - (pamafive + 1), len));
    39                 }
    40             } else {
    41                 int pamafour = len - 2;
    42                 stringBuilder.append(value.substring(0, 1));
    43                 for (int i = 0; i < pamafour; i++) {
    44                     stringBuilder.append(SYMBOL);
    45                 }
    46                 stringBuilder.append(value.substring(len - 1, len));
    47             }
    48         }
    49         return stringBuilder.toString();
    50     }
    View Code

     XML工具

     1 import com.thoughtworks.xstream.XStream;
     2 import com.thoughtworks.xstream.io.xml.DomDriver;
     3 import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;
     4 import java.io.InputStream;
     5 
     6 public class XmlUtil {
     7     
     8     /**
     9      * 
    10      * @Description: xml字符串转换为对象
    11      * @param inputXml
    12      * @param type
    13      * @return
    14      * @throws Exception
    15      */
    16     public static Object xml2Object(String inputXml, Class<?> type) throws Exception {
    17         if (null == inputXml || "".equals(inputXml)) {
    18             return null;
    19         }
    20         XStream xstream = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));
    21         xstream.alias("xml", type);
    22         return xstream.fromXML(inputXml);
    23     }
    24     
    25     /**
    26      * 
    27      * @Description: 从inputStream中读取对象
    28      * @param inputStream
    29      * @param type
    30      * @return
    31      * @throws Exception
    32      */
    33     public static Object xml2Object(InputStream inputStream, Class<?> type) throws Exception {
    34         if (null == inputStream) {
    35             return null;
    36         }
    37         XStream xstream = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));
    38         xstream.alias("xml", type);
    39         return xstream.fromXML(inputStream, type);
    40     }
    41 
    42     /**
    43      * 
    44      * @Description: 对象转换为xml字符串
    45      * @param ro
    46      * @param types
    47      * @return
    48      * @throws Exception
    49      */
    50     public static String object2Xml(Object ro, Class<?> types) throws Exception {
    51         if (null == ro) {
    52             return null;
    53         }
    54         XStream xstream = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));
    55         xstream.alias("xml", types);
    56         return xstream.toXML(ro);
    57     }
    58     
    59 }
    View Code

     Cookie工具类

      1 import org.slf4j.Logger;
      2 import org.slf4j.LoggerFactory;
      3 import javax.servlet.http.Cookie;
      4 import javax.servlet.http.HttpServletRequest;
      5 import javax.servlet.http.HttpServletResponse;
      6 import java.io.UnsupportedEncodingException;
      7 import java.net.URLDecoder;
      8 import java.net.URLEncoder;
      9 
     10 
     11 /**
     12  * 
     13  * @Title: CookieUtils.java
     14  * @Description: Cookie 工具类
     15  */
     16 public final class CookieUtils {
     17 
     18     final static Logger logger = LoggerFactory.getLogger(CookieUtils.class);
     19     
     20     /**
     21      * 
     22      * @Description: 得到Cookie的值, 不编码
     23      * @param request
     24      * @param cookieName
     25      * @return
     26      */
     27     public static String getCookieValue(HttpServletRequest request, String cookieName) {
     28         return getCookieValue(request, cookieName, false);
     29     }
     30     
     31     /**
     32      * 
     33      * @Description: 得到Cookie的值
     34      * @param request
     35      * @param cookieName
     36      * @param isDecoder
     37      * @return
     38      */
     39     public static String getCookieValue(HttpServletRequest request, String cookieName, boolean isDecoder) {
     40         Cookie[] cookieList = request.getCookies();
     41         if (cookieList == null || cookieName == null) {
     42             return null;
     43         }
     44         String retValue = null;
     45         try {
     46             for (int i = 0; i < cookieList.length; i++) {
     47                 if (cookieList[i].getName().equals(cookieName)) {
     48                     if (isDecoder) {
     49                         retValue = URLDecoder.decode(cookieList[i].getValue(), "UTF-8");
     50                     } else {
     51                         retValue = cookieList[i].getValue();
     52                     }
     53                     break;
     54                 }
     55             }
     56         } catch (UnsupportedEncodingException e) {
     57             e.printStackTrace();
     58         }
     59         return retValue;
     60     }
     61 
     62     /**
     63      * 
     64      * @Description: 得到Cookie的值
     65      * @param request
     66      * @param cookieName
     67      * @param encodeString
     68      * @return
     69      */
     70     public static String getCookieValue(HttpServletRequest request, String cookieName, String encodeString) {
     71         Cookie[] cookieList = request.getCookies();
     72         if (cookieList == null || cookieName == null) {
     73             return null;
     74         }
     75         String retValue = null;
     76         try {
     77             for (int i = 0; i < cookieList.length; i++) {
     78                 if (cookieList[i].getName().equals(cookieName)) {
     79                     retValue = URLDecoder.decode(cookieList[i].getValue(), encodeString);
     80                     break;
     81                 }
     82             }
     83         } catch (UnsupportedEncodingException e) {
     84              e.printStackTrace();
     85         }
     86         return retValue;
     87     }
     88 
     89     /**
     90      * 
     91      * @Description: 设置Cookie的值 不设置生效时间默认浏览器关闭即失效,也不编码
     92      * @param request
     93      * @param response
     94      * @param cookieName
     95      * @param cookieValue
     96      */
     97     public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
     98             String cookieValue) {
     99         setCookie(request, response, cookieName, cookieValue, -1);
    100     }
    101 
    102     /**
    103      * 
    104      * @Description: 设置Cookie的值 在指定时间内生效,但不编码
    105      * @param request
    106      * @param response
    107      * @param cookieName
    108      * @param cookieValue
    109      * @param cookieMaxage
    110      */
    111     public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
    112             String cookieValue, int cookieMaxage) {
    113         setCookie(request, response, cookieName, cookieValue, cookieMaxage, false);
    114     }
    115 
    116     /**
    117      * 
    118      * @Description: 设置Cookie的值 不设置生效时间,但编码
    119      * 在服务器被创建,返回给客户端,并且保存客户端
    120      * 如果设置了SETMAXAGE(int seconds),会把cookie保存在客户端的硬盘中
    121      * 如果没有设置,会默认把cookie保存在浏览器的内存中
    122      * 一旦设置setPath():只能通过设置的路径才能获取到当前的cookie信息
    123      * @param request
    124      * @param response
    125      * @param cookieName
    126      * @param cookieValue
    127      * @param isEncode
    128      */
    129     public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
    130             String cookieValue, boolean isEncode) {
    131         setCookie(request, response, cookieName, cookieValue, -1, isEncode);
    132     }
    133 
    134    /**
    135     * 
    136     * @Description: 设置Cookie的值 在指定时间内生效, 编码参数
    137     * @param request
    138     * @param response
    139     * @param cookieName
    140     * @param cookieValue
    141     * @param cookieMaxage
    142     * @param isEncode
    143     */
    144     public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
    145             String cookieValue, int cookieMaxage, boolean isEncode) {
    146         doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, isEncode);
    147     }
    148 
    149     /**
    150      * 
    151      * @Description: 设置Cookie的值 在指定时间内生效, 编码参数(指定编码)
    152      * @param request
    153      * @param response
    154      * @param cookieName
    155      * @param cookieValue
    156      * @param cookieMaxage
    157      * @param encodeString
    158      */
    159     public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
    160             String cookieValue, int cookieMaxage, String encodeString) {
    161         doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, encodeString);
    162     }
    163 
    164     /**
    165      * 
    166      * @Description: 删除Cookie带cookie域名
    167      * @param request
    168      * @param response
    169      * @param cookieName
    170      */
    171     public static void deleteCookie(HttpServletRequest request, HttpServletResponse response,
    172             String cookieName) {
    173         doSetCookie(request, response, cookieName, null, -1, false);
    174 //        doSetCookie(request, response, cookieName, "", -1, false);
    175     }
    176 
    177     
    178     /**
    179      * 
    180      * @Description: 设置Cookie的值,并使其在指定时间内生效
    181      * @param request
    182      * @param response
    183      * @param cookieName
    184      * @param cookieValue
    185      * @param cookieMaxage    cookie生效的最大秒数
    186      * @param isEncode
    187      */
    188     private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response,
    189             String cookieName, String cookieValue, int cookieMaxage, boolean isEncode) {
    190         try {
    191             if (cookieValue == null) {
    192                 cookieValue = "";
    193             } else if (isEncode) {
    194                 cookieValue = URLEncoder.encode(cookieValue, "utf-8");
    195             }
    196             Cookie cookie = new Cookie(cookieName, cookieValue);
    197             if (cookieMaxage > 0)
    198                 cookie.setMaxAge(cookieMaxage);
    199             if (null != request) {// 设置域名的cookie
    200                 String domainName = getDomainName(request);
    201                 logger.info("========== domainName: {} ==========", domainName);
    202                 if (!"localhost".equals(domainName)) {
    203                     cookie.setDomain(domainName);
    204                 }
    205             }
    206             cookie.setPath("/");
    207             response.addCookie(cookie);
    208         } catch (Exception e) {
    209              e.printStackTrace();
    210         }
    211     }
    212 
    213     /**
    214      * 
    215      * @Description: 设置Cookie的值,并使其在指定时间内生效
    216      * @param request
    217      * @param response
    218      * @param cookieName
    219      * @param cookieValue
    220      * @param cookieMaxage    cookie生效的最大秒数
    221      * @param encodeString
    222      */
    223     private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response,
    224             String cookieName, String cookieValue, int cookieMaxage, String encodeString) {
    225         try {
    226             if (cookieValue == null) {
    227                 cookieValue = "";
    228             } else {
    229                 cookieValue = URLEncoder.encode(cookieValue, encodeString);
    230             }
    231             Cookie cookie = new Cookie(cookieName, cookieValue);
    232             if (cookieMaxage > 0)
    233                 cookie.setMaxAge(cookieMaxage);
    234             if (null != request) {// 设置域名的cookie
    235                 String domainName = getDomainName(request);
    236                 logger.info("========== domainName: {} ==========", domainName);
    237                 if (!"localhost".equals(domainName)) {
    238                     cookie.setDomain(domainName);
    239                 }
    240             }
    241             cookie.setPath("/");
    242             response.addCookie(cookie);
    243         } catch (Exception e) {
    244              e.printStackTrace();
    245         }
    246     }
    247 
    248     /**
    249      * 
    250      * @Description: 得到cookie的域名
    251      * @return
    252      */
    253     private static final String getDomainName(HttpServletRequest request) {
    254         String domainName = null;
    255 
    256         String serverName = request.getRequestURL().toString();
    257         if (serverName == null || serverName.equals("")) {
    258             domainName = "";
    259         } else {
    260             serverName = serverName.toLowerCase();
    261             serverName = serverName.substring(7);
    262             final int end = serverName.indexOf("/");
    263             serverName = serverName.substring(0, end);
    264             if (serverName.indexOf(":") > 0) {
    265                 String[] ary = serverName.split("\:");
    266                 serverName = ary[0];
    267             }
    268 
    269             final String[] domains = serverName.split("\.");
    270             int len = domains.length;
    271             if (len > 3 && !isIp(serverName)) {
    272                 // www.xxx.com.cn
    273                 domainName = "." + domains[len - 3] + "." + domains[len - 2] + "." + domains[len - 1];
    274             } else if (len <= 3 && len > 1) {
    275                 // xxx.com or xxx.cn
    276                 domainName = "." + domains[len - 2] + "." + domains[len - 1];
    277             } else {
    278                 domainName = serverName;
    279             }
    280         }
    281         return domainName;
    282     }
    283     
    284     public static String trimSpaces(String IP){//去掉IP字符串前后所有的空格  
    285         while(IP.startsWith(" ")){  
    286                IP= IP.substring(1,IP.length()).trim();  
    287             }  
    288         while(IP.endsWith(" ")){  
    289                IP= IP.substring(0,IP.length()-1).trim();  
    290             }  
    291         return IP;  
    292     }  
    293     
    294     public static boolean isIp(String IP){//判断是否是一个IP  
    295         boolean b = false;  
    296         IP = trimSpaces(IP);  
    297         if(IP.matches("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")){  
    298             String s[] = IP.split("\.");  
    299             if(Integer.parseInt(s[0])<255)  
    300                 if(Integer.parseInt(s[1])<255)  
    301                     if(Integer.parseInt(s[2])<255)  
    302                         if(Integer.parseInt(s[3])<255)  
    303                             b = true;  
    304         }  
    305         return b;  
    306     }  
    307 
    308 }
    View Code

     获取URL参数

     1     /**
     2      * 将url参数转为map
     3      * @param param
     4      * @return java.util.Map<java.lang.String,java.lang.String>
     5      */
     6     public static Map<String, String> getUrlParams(String param) throws Exception {
     7         Map<String, String> map = new LinkedHashMap<>(0);
     8         if (StringUtils.isBlank(param)) {
     9             return map;
    10         }
    11         if(param.contains("?")){
    12             param=param.split("\?")[1];
    13         }
    14         String[] params = param.split("&");
    15         for (int i = 0; i < params.length; i++) {
    16             String[] p = params[i].split("=");
    17             if (p.length == 2) {
    18                 map.put(p[0], URLEncoder.encode(p[1],"utf-8"));
    19             }
    20         }
    21         return map;
    22     }
    View Code

      

  • 相关阅读:
    简单区间dp
    【题解】石子合并
    【2019.7.6】刷题记录
    【题解】大朋友的数字
    【基础】dp系列1
    【题解】垃圾陷阱
    【题解】导弹拦截
    hadoop各组件安装(非专业人士,不定期更新)
    python逼格提升
    python第三十二天-----算法
  • 原文地址:https://www.cnblogs.com/huozhonghun/p/13772934.html
Copyright © 2020-2023  润新知