CommonUtil.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package com.template.common.utils;
  2. import lombok.extern.slf4j.Slf4j;
  3. import javax.servlet.http.HttpServletRequest;
  4. import java.net.InetAddress;
  5. import java.net.UnknownHostException;
  6. import java.security.MessageDigest;
  7. import java.time.LocalDate;
  8. import java.time.LocalDateTime;
  9. import java.time.LocalTime;
  10. import java.util.*;
  11. /**
  12. * 公共工具类
  13. **/
  14. @Slf4j
  15. public class CommonUtil {
  16. /**
  17. * 获取ip
  18. *
  19. * @param request
  20. * @return
  21. */
  22. public static String getIpAddr(HttpServletRequest request) {
  23. String ipAddress = null;
  24. try {
  25. ipAddress = request.getHeader("x-forwarded-for");
  26. System.out.println("x-forwarded-for="+ipAddress);
  27. if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
  28. ipAddress = request.getHeader("Proxy-Client-IP");
  29. System.out.println("Proxy-Client-IP="+ipAddress);
  30. }
  31. if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
  32. ipAddress = request.getHeader("WL-Proxy-Client-IP");
  33. System.out.println("WL-Proxy-Client-IP="+ipAddress);
  34. }
  35. if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
  36. ipAddress = request.getRemoteAddr();
  37. System.out.println("getRemoteAddr="+ipAddress);
  38. if (ipAddress.equals("127.0.0.1")) {
  39. // 根据网卡取本机配置的IP
  40. InetAddress inet = null;
  41. try {
  42. inet = InetAddress.getLocalHost();
  43. } catch (UnknownHostException e) {
  44. e.printStackTrace();
  45. }
  46. ipAddress = inet.getHostAddress();
  47. System.out.println("getHostAddress="+ipAddress);
  48. }
  49. }
  50. // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
  51. if (ipAddress != null && ipAddress.length() > 15) {
  52. // "***.***.***.***".length()
  53. // = 15
  54. if (ipAddress.indexOf(",") > 0) {
  55. ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
  56. }
  57. }
  58. } catch (Exception e) {
  59. ipAddress = "";
  60. }
  61. return ipAddress;
  62. }
  63. /**
  64. * 获取全部请求头
  65. *
  66. * @param request
  67. * @return
  68. */
  69. public static Map<String, String> getAllRequestHeader(HttpServletRequest request) {
  70. Enumeration<String> headerNames = request.getHeaderNames();
  71. Map<String, String> map = new HashMap<>();
  72. while (headerNames.hasMoreElements()) {
  73. String key = headerNames.nextElement();
  74. //根据名称获取请求头的值
  75. String value = request.getHeader(key);
  76. map.put(key, value);
  77. }
  78. return map;
  79. }
  80. /**
  81. * MD5加密
  82. *
  83. * @param data
  84. * @return
  85. */
  86. public static String MD5(String data) {
  87. try {
  88. MessageDigest md = MessageDigest.getInstance("MD5");
  89. byte[] array = md.digest(data.getBytes("UTF-8"));
  90. StringBuilder sb = new StringBuilder();
  91. for (byte item : array) {
  92. sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
  93. }
  94. return sb.toString().toUpperCase();
  95. } catch (Exception exception) {
  96. }
  97. return null;
  98. }
  99. /**
  100. * 获取验证码随机数
  101. *
  102. * @param length
  103. * @return
  104. */
  105. public static String getRandomCode(int length) {
  106. String sources = "0123456789";
  107. Random random = new Random();
  108. StringBuilder sb = new StringBuilder();
  109. for (int j = 0; j < length; j++) {
  110. sb.append(sources.charAt(random.nextInt(9)));
  111. }
  112. return sb.toString();
  113. }
  114. /**
  115. * 获取当前时间戳
  116. *
  117. * @return
  118. */
  119. public static long getCurrentTimestamp() {
  120. return System.currentTimeMillis();
  121. }
  122. /**
  123. * 生成uuid
  124. *
  125. * @return
  126. */
  127. public static String generateUUID() {
  128. return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32);
  129. }
  130. /**
  131. * 获取随机长度的串
  132. *
  133. * @param length
  134. * @return
  135. */
  136. private static final String ALL_CHAR_NUM = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  137. public static String getStringNumRandom(int length) {
  138. //生成随机数字和字母,
  139. Random random = new Random();
  140. StringBuilder saltString = new StringBuilder(length);
  141. for (int i = 1; i <= length; ++i) {
  142. saltString.append(ALL_CHAR_NUM.charAt(random.nextInt(ALL_CHAR_NUM.length())));
  143. }
  144. return saltString.toString();
  145. }
  146. /**
  147. * LocalDate 拼接时间转成 LocalDateTime
  148. * @param localDate
  149. * @param timeStr HH:mm:ss
  150. * @return
  151. */
  152. public static LocalDateTime localDateToTime(LocalDate localDate, String timeStr) {
  153. LocalTime time = LocalTime.parse(timeStr);
  154. return LocalDateTime.of(localDate.getYear(), localDate.getMonth(), localDate.getDayOfMonth(),
  155. time.getHour(), time.getMinute(), time.getSecond());
  156. }
  157. }