ToolsUtils.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. package com.zhongzheng.common.utils;
  2. import cn.hutool.core.lang.Validator;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
  5. import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
  6. import com.aliyun.teaopenapi.models.Config;
  7. import com.google.zxing.common.BitMatrix;
  8. import com.zhongzheng.common.core.domain.AjaxResult;
  9. import io.micrometer.core.lang.NonNull;
  10. import net.sf.jsqlparser.expression.LongValue;
  11. import java.awt.image.BufferedImage;
  12. import java.io.*;
  13. import java.nio.charset.StandardCharsets;
  14. import java.security.MessageDigest;
  15. import java.security.NoSuchAlgorithmException;
  16. import java.util.*;
  17. import java.util.regex.Matcher;
  18. import java.util.regex.Pattern;
  19. import java.util.zip.ZipEntry;
  20. import java.util.zip.ZipOutputStream;
  21. public class ToolsUtils {
  22. public static final int EMU_PER_PX = 9525;
  23. /**
  24. * 获取模块编码参数
  25. */
  26. public static String getSmsCode()
  27. {
  28. Random random = new Random();
  29. String result="";
  30. for (int i=0;i<6;i++)
  31. {
  32. result+=random.nextInt(10);
  33. }
  34. return result;
  35. }
  36. public static String join(@NonNull CharSequence delimiter, @NonNull Iterable tokens) {
  37. final Iterator<?> it = tokens.iterator();
  38. if (!it.hasNext()) {
  39. return "";
  40. }
  41. final StringBuilder sb = new StringBuilder();
  42. sb.append(it.next());
  43. while (it.hasNext()) {
  44. sb.append(delimiter);
  45. sb.append(it.next());
  46. }
  47. return sb.toString();
  48. }
  49. public static final int emuToPx(double emu) {
  50. return DoubleUtils.div(emu, EMU_PER_PX).intValue();
  51. }
  52. public static String getEncoding(String str) {
  53. String encode = "GB2312";
  54. try {
  55. if (str.equals(new String(str.getBytes(encode), encode))) {
  56. String s = encode;
  57. return s;
  58. }
  59. } catch (Exception exception) {
  60. }
  61. encode = "ISO-8859-1";
  62. try {
  63. if (str.equals(new String(str.getBytes(encode), encode))) {
  64. String s1 = encode;
  65. return s1;
  66. }
  67. } catch (Exception exception1) {
  68. }
  69. encode = "UTF-8";
  70. try {
  71. if (str.equals(new String(str.getBytes(encode), encode))) {
  72. String s2 = encode;
  73. return s2;
  74. }
  75. } catch (Exception exception2) {
  76. }
  77. encode = "GBK";
  78. try {
  79. if (str.equals(new String(str.getBytes(encode), encode))) {
  80. String s3 = encode;
  81. return s3;
  82. }
  83. } catch (Exception exception3) {
  84. }
  85. return "";
  86. }
  87. /**
  88. * 字符串转换UTF-8编码
  89. *
  90. * @param string 字符串
  91. * @return java.lang.String
  92. * @date 2022/4/14.
  93. */
  94. public static String toUtf8String(String string) {
  95. StringBuilder stringBuffer = new StringBuilder();
  96. for (int i = 0; i < string.length(); i++) {
  97. char c = string.charAt(i);
  98. if (c <= 255) {
  99. stringBuffer.append(c);
  100. } else {
  101. byte[] b;
  102. try {
  103. b = Character.toString(c).getBytes(StandardCharsets.UTF_8);
  104. } catch (Exception ex) {
  105. b = new byte[0];
  106. }
  107. for (int value : b) {
  108. int k = value;
  109. if (k < 0) k += 256;
  110. stringBuffer.append("%").append(Integer.toHexString(k).toUpperCase());
  111. }
  112. }
  113. }
  114. return stringBuffer.toString();
  115. }
  116. public static String StringToMd5(String psw) {
  117. {
  118. try {
  119. MessageDigest md5 = MessageDigest.getInstance("MD5");
  120. md5.update(psw.getBytes("UTF-8"));
  121. byte[] encryption = md5.digest();
  122. StringBuffer strBuf = new StringBuffer();
  123. for (int i = 0; i < encryption.length; i++) {
  124. if (Integer.toHexString(0xff & encryption[i]).length() == 1) {
  125. strBuf.append("0").append(Integer.toHexString(0xff & encryption[i]));
  126. } else {
  127. strBuf.append(Integer.toHexString(0xff & encryption[i]));
  128. }
  129. }
  130. return strBuf.toString();
  131. } catch (NoSuchAlgorithmException e) {
  132. return "";
  133. } catch (UnsupportedEncodingException e) {
  134. return "";
  135. }
  136. }
  137. }
  138. public static String getCharAndNumr(int length) {
  139. Random random = new Random();
  140. StringBuffer valSb = new StringBuffer();
  141. String charStr = "0123456789abcdefghijklmnopqrstuvwxyz";
  142. int charLength = charStr.length();
  143. for (int i = 0; i < length; i++) {
  144. int index = random.nextInt(charLength);
  145. valSb.append(charStr.charAt(index));
  146. }
  147. return valSb.toString();
  148. }
  149. public static <T> List<List<T>> splitListBycapacity(List<T> source, int capacity){
  150. List<List<T>> result=new ArrayList<List<T>>();
  151. if (source != null){
  152. int size = source.size();
  153. if (size > 0 ){
  154. for (int i = 0; i < size;) {
  155. List<T> value = null;
  156. int end = i+capacity;
  157. if (end > size){
  158. end = size;
  159. }
  160. value = source.subList(i,end);
  161. i = end;
  162. result.add(value);
  163. }
  164. }else {
  165. result = null;
  166. }
  167. }else {
  168. result = null;
  169. }
  170. return result;
  171. }
  172. /**
  173. * 校验签名
  174. *
  175. * @param token token
  176. * @param signature 签名
  177. * @param timestamp 时间戳
  178. * @param nonce 随机数
  179. * @return 布尔值
  180. */
  181. public static boolean checkGzhServerSignature(String token,String signature, String timestamp, String nonce) {
  182. String checktext = null;
  183. if (null != signature) {
  184. //对ToKen,timestamp,nonce 按字典排序
  185. String[] paramArr = new String[]{token, timestamp, nonce};
  186. Arrays.sort(paramArr);
  187. //将排序后的结果拼成一个字符串
  188. String content = paramArr[0].concat(paramArr[1]).concat(paramArr[2]);
  189. try {
  190. MessageDigest md = MessageDigest.getInstance("SHA-1");
  191. //对接后的字符串进行sha1加密
  192. byte[] digest = md.digest(content.toString().getBytes());
  193. checktext = byteToStr(digest);
  194. } catch (NoSuchAlgorithmException e) {
  195. e.printStackTrace();
  196. }
  197. }
  198. //将加密后的字符串与signature进行对比
  199. return checktext != null ? checktext.equals(signature.toUpperCase()) : false;
  200. }
  201. /**
  202. * 将字节数组转化我16进制字符串
  203. *
  204. * @param byteArrays 字符数组
  205. * @return 字符串
  206. */
  207. private static String byteToStr(byte[] byteArrays) {
  208. String str = "";
  209. for (int i = 0; i < byteArrays.length; i++) {
  210. str += byteToHexStr(byteArrays[i]);
  211. }
  212. return str;
  213. }
  214. /**
  215. * 将字节转化为十六进制字符串
  216. *
  217. * @param myByte 字节
  218. * @return 字符串
  219. */
  220. private static String byteToHexStr(byte myByte) {
  221. char[] Digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  222. char[] tampArr = new char[2];
  223. tampArr[0] = Digit[(myByte >>> 4) & 0X0F];
  224. tampArr[1] = Digit[myByte & 0X0F];
  225. String str = new String(tampArr);
  226. return str;
  227. }
  228. /**
  229. * 将文件转换成Byte数组
  230. */
  231. public static byte[] getBytesByFile(String pathStr) {
  232. File file = new File(pathStr);
  233. try {
  234. FileInputStream fis = new FileInputStream(file);
  235. ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
  236. byte[] b = new byte[1000];
  237. int n;
  238. while ((n = fis.read(b)) != -1) {
  239. bos.write(b, 0, n);
  240. }
  241. fis.close();
  242. byte[] data = bos.toByteArray();
  243. bos.close();
  244. return data;
  245. } catch (IOException e) {
  246. }
  247. return null;
  248. }
  249. /**
  250. * 不够位数的在前面补0,保留num的长度位数字
  251. * @param code
  252. * @return
  253. */
  254. public static String autoGenericCode(String code, int num) {
  255. String result = "";
  256. // 保留num的位数
  257. // 0 代表前面补充0
  258. // num 代表长度为4
  259. // d 代表参数为正数型
  260. result = String.format("%0" + num + "d", Integer.parseInt(code));
  261. return result;
  262. }
  263. public static Boolean checkSignFromOldSys(String stamp,String sign) {
  264. String newSign = stamp+"pubilc2022";
  265. if(!sign.equals(ToolsUtils.EncoderByMd5(newSign))){
  266. return false;
  267. }
  268. if((Long.parseLong(stamp)+10L>(DateUtils.getNowTime().longValue()))&&(Long.parseLong(stamp)<(DateUtils.getNowTime().longValue()-10L))){
  269. return false;
  270. }
  271. return true;
  272. }
  273. public static String EncoderByMd5(String str) {
  274. String result = "";
  275. MessageDigest md5 = null;
  276. try {
  277. md5 = MessageDigest.getInstance("MD5");
  278. // 这句是关键
  279. md5.update(str.getBytes("gb2312"));
  280. } catch (NoSuchAlgorithmException e) {
  281. // TODO Auto-generated catch block
  282. e.printStackTrace();
  283. } catch (UnsupportedEncodingException e) {
  284. // TODO Auto-generated catch block
  285. e.printStackTrace();
  286. }
  287. byte b[] = md5.digest();
  288. int i;
  289. StringBuffer buf = new StringBuffer("");
  290. for (int offset = 0; offset < b.length; offset++) {
  291. i = b[offset];
  292. if (i < 0)
  293. i += 256;
  294. if (i < 16)
  295. buf.append("0");
  296. buf.append(Integer.toHexString(i));
  297. }
  298. result = buf.toString();
  299. return result;
  300. }
  301. public static boolean verifPwd(String passWord) {
  302. if(Validator.isEmpty(passWord)){
  303. return false;
  304. }
  305. /*if(passWord.length()<8||passWord.length()>18){
  306. return false;
  307. }*/
  308. String regExp = "^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\\d)(?=.*?[!#@*&.])[a-zA-Z\\d!#@*&.]{8,16}$";
  309. Pattern p = Pattern.compile(regExp);
  310. Matcher m = p.matcher(passWord);
  311. if (m.matches()){
  312. return true;
  313. } else {
  314. return false;
  315. }
  316. }
  317. public static String getTenantId() {
  318. String TenantId = ServletUtils.getRequest().getHeader("TenantId");
  319. if(!StrUtil.isNotBlank(TenantId)||TenantId==null){
  320. TenantId = ServletUtils.getResponse().getHeader("TenantId");
  321. }
  322. return TenantId;
  323. }
  324. private static int getRandom(int count) {
  325. return (int) Math.round(Math.random() * (count));
  326. }
  327. public static String getRandomString(int length){
  328. String string = "abcdefghijklmnopqrstuvwxyz";
  329. StringBuffer sb = new StringBuffer();
  330. int len = string.length();
  331. for (int i = 0; i < length; i++) {
  332. sb.append(string.charAt(getRandom(len-1)));
  333. }
  334. return sb.toString();
  335. }
  336. public static BufferedImage toBufferedImage(BitMatrix matrix) {
  337. int black = 0xFF000000;
  338. int white = 0x00FFFFFF;
  339. int width = matrix.getWidth();
  340. int height = matrix.getHeight();
  341. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  342. for (int x = 0; x < width; x++) {
  343. for (int y = 0; y < height; y++) {
  344. image.setRGB(x, y, matrix.get(x, y) ? black : white);
  345. }
  346. }
  347. int imgHeight = image.getHeight();//取得图片的长和宽
  348. int imgWidth = image.getWidth();
  349. int c = image.getRGB(3, 3);
  350. int alpha = 10;
  351. //防止越位
  352. if (alpha < 0) {
  353. alpha = 0;
  354. } else if (alpha > 10) {
  355. alpha = 10;
  356. }
  357. BufferedImage tmpImg = new BufferedImage(imgWidth, imgHeight,BufferedImage.TYPE_4BYTE_ABGR);//新建一个类型支持透明的BufferedImage
  358. for(int i = 0; i < imgWidth; ++i)//把原图片的内容复制到新的图片,同时把背景设为透明
  359. {
  360. for(int j = 0; j < imgHeight; ++j){
  361. //把背景设为透明
  362. if(image.getRGB(i, j) == c){
  363. tmpImg .setRGB(i, j, c & 0x00ffffff);
  364. }
  365. //设置透明度
  366. else{
  367. int rgb = tmpImg .getRGB(i, j);
  368. rgb = ((alpha * 255 / 10) << 24) | (rgb & 0x00ffffff);
  369. tmpImg .setRGB(i, j, rgb);
  370. }
  371. }
  372. }
  373. return tmpImg ;
  374. }
  375. }