MD5工具函数
在存储用户一些敏感信息的时候,需要考虑将这部分敏感信息通过md5加密以后再存储到数据库中,防止用户的敏感信息暴露。
public static String MD5(String data) {try {java.security.MessageDigest md = MessageDigest.getInstance("MD5");byte[] array = md.digest(data.getBytes("UTF-8"));StringBuilder sb = new StringBuilder();for (byte item : array) {sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));}return sb.toString().toUpperCase();} catch (Exception exception) {}return null;}
从request请求中获取用户的ip地址
public static String getIpAddr(HttpServletRequest request) {String ipAddress = null;try {ipAddress = request.getHeader("x-forwarded-for");if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {ipAddress = request.getHeader("Proxy-Client-IP");}if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {ipAddress = request.getHeader("WL-Proxy-Client-IP");}if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {ipAddress = request.getRemoteAddr();if (ipAddress.equals("127.0.0.1")) {// 根据网卡取本机配置的IPInetAddress inet = null;try {inet = InetAddress.getLocalHost();} catch (UnknownHostException e) {e.printStackTrace();}ipAddress = inet.getHostAddress();}}// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割if (ipAddress != null && ipAddress.length() > 15) {// "***.***.***.***".length()// = 15if (ipAddress.indexOf(",") > 0) {ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));}}} catch (Exception e) {ipAddress="";}return ipAddress;}
Json字符串和对象之间的相互解析
import com.fasterxml.jackson.databind.ObjectMapper;public class JsonUtil {private static final ObjectMapper MAPPER = new ObjectMapper();/*** 把对象转字符串* @param data* @return*/public static String objectToJson(Object data){try {return MAPPER.writeValueAsString(data);}catch (Exception e){e.printStackTrace();}return null;}/*** json字符串转对象* @param jsonData* @param beanType* @param <T>* @return*/public static <T> T jsonToPojo(String jsonData, Class<T> beanType){try {T t = MAPPER.readValue(jsonData,beanType);return t;}catch (Exception e){e.printStackTrace();}return null;}}
验证一个字符串是否是邮箱或手机号码
import java.util.regex.Matcher;import java.util.regex.Pattern;/*** 邮箱手机号码验证工具类*/public class CheckUtil {/*** 邮箱正则*/private static final Pattern MAIL_PATTERN = Pattern.compile("^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");/*** 手机号正则,暂时未用*/private static final Pattern PHONE_PATTERN = Pattern.compile("^((1[3-9][0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");/*** @param email* @return*/public static boolean isEmail(String email) {if (null == email || "".equals(email)) {return false;}Matcher m = MAIL_PATTERN.matcher(email);return m.matches();}/*** 暂时未用* @param phone* @return*/public static boolean isPhone(String phone) {if (null == phone || "".equals(phone)) {return false;}Matcher m = PHONE_PATTERN.matcher(phone);return m.matches();}}生产指定长度的随机验证码
/*** 获取随机length长度的验证码* @param length* @return*/public static String getRandomCode(int length){//验证码字符集String sources = "0123456789";Random random = new Random();StringBuilder sb = new StringBuilder();for(int j=0; j<length; j++){sb.append(sources.charAt(random.nextInt(9)));}return sb.toString();}
时间类型和字符串类型的转换
public class TimeUtil {/*** 默认日期格式*/private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";/*** 默认日期格式*/private static final DateTimeFormatter DEFAULT_DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(DEFAULT_PATTERN);private static final ZoneId DEFAULT_ZONE_ID = ZoneId.systemDefault();/*** LocalDateTime 转 字符串,指定日期格式* @param time* @param pattern* @return*/public static String format(LocalDateTime localDateTime, String pattern){DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);String timeStr = formatter.format(localDateTime.atZone(DEFAULT_ZONE_ID));return timeStr;}/*** Date 转 字符串, 指定日期格式* @param time* @param pattern* @return*/public static String format(Date time, String pattern){DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);String timeStr = formatter.format(time.toInstant().atZone(DEFAULT_ZONE_ID));return timeStr;}/*** Date 转 字符串,默认日期格式* @param time* @return*/public static String format(Date time){String timeStr = DEFAULT_DATE_TIME_FORMATTER.format(time.toInstant().atZone(DEFAULT_ZONE_ID));return timeStr;}/*** timestamp 转 字符串,默认日期格式** @param time* @return*/public static String format(long timestamp) {String timeStr = DEFAULT_DATE_TIME_FORMATTER.format(new Date(timestamp).toInstant().atZone(DEFAULT_ZONE_ID));return timeStr;}/*** 字符串 转 Date** @param time* @return*/public static Date strToDate(String time) {LocalDateTime localDateTime = LocalDateTime.parse(time, DEFAULT_DATE_TIME_FORMATTER);return Date.from(localDateTime.atZone(DEFAULT_ZONE_ID).toInstant());}/*** 获取当天剩余的秒数,用于流量包过期配置* @param currentDate* @return*/public static Integer getRemainSecondsOneDay(Date currentDate) {LocalDateTime midnight = LocalDateTime.ofInstant(currentDate.toInstant(),ZoneId.systemDefault()).plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);LocalDateTime currentDateTime = LocalDateTime.ofInstant(currentDate.toInstant(),ZoneId.systemDefault());long seconds = ChronoUnit.SECONDS.between(currentDateTime, midnight);return (int) seconds;}}
文章转载自梁霖编程工具库,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




