OtherUtil.java 1.42 KB
Newer Older
刘基明's avatar
刘基明 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
package com.tanpu.community.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

public class OtherUtil {

    private final static String numberPattern = "^[0-9]*[1-9][0-9]*$";
    private final static String phonePattern = "(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}";
    private final static String emailPattern = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.com";


    public static String getRequestUrl(HttpServletRequest request) {
        String s = request.getRequestURL().toString();
        String querystr = request.getQueryString();
        if (StringUtils.isNotEmpty(querystr)) {
            s = s + "?" + querystr;
        }
        return s;
    }

    public static String blockPhoneAndEmail(String line) {
        // 屏蔽手机号
        line = line.replaceAll(phonePattern,"**********");
        // 屏蔽邮箱
        line =  line.replaceAll(emailPattern,"*@*.com");
        return line;
    }


    /**
     * 脱敏手机号
     *
     * @param mobile
     * @return
     */
    public static String formatMobile(String mobile) {
        if (StringUtils.isNotBlank(mobile)) {
            mobile = mobile.replaceAll("(\\w{3})\\w*(\\w{4})", "$1****$2");
        }
        return mobile;
    }

    public static void main(String[] args) {
        System.out.println(OtherUtil.blockPhoneAndEmail("我的手机号是18621088081!,邮箱是123@qq.com。"));
    }
}