ToolsUtils.java 13 KB

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