CommonUtil.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package org.springblade.common.utils;
  2. import cn.hutool.core.io.FileUtil;
  3. import org.apache.commons.lang.StringUtils;
  4. import org.springframework.util.CollectionUtils;
  5. import java.io.*;
  6. import java.math.BigDecimal;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9. import java.util.*;
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12. import java.util.stream.Collectors;
  13. import java.util.stream.Stream;
  14. /**
  15. * 通用工具类
  16. *
  17. * @author Chill
  18. */
  19. public class CommonUtil {
  20. public static Boolean checkBigDecimal(Object value){
  21. try{
  22. if(value != null && StringUtils.isNotEmpty(value.toString())){
  23. new BigDecimal(value.toString());
  24. return true;
  25. }
  26. }catch (Exception e){
  27. e.printStackTrace();
  28. }
  29. return false;
  30. }
  31. public static void removeFile(List<String> removeList){
  32. for(String fileUrl : removeList){
  33. try{
  34. FileUtil.del(new File(fileUrl));
  35. }catch (Exception e){
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  40. public static String handleNull(Object obj) {
  41. if (null == obj) {
  42. return "";
  43. } else {
  44. return obj.toString().trim();
  45. }
  46. }
  47. public static String join(Object ...args){
  48. if(args!=null){
  49. if(args.length>2){
  50. List<String> list = Arrays.stream(args).limit(args.length - 1).map(CommonUtil::handleNull).collect(Collectors.toList());
  51. String split=handleNull(args[args.length-1]);
  52. return join(list, split);
  53. }else{
  54. return handleNull(args[0]);
  55. }
  56. }else {
  57. return "";
  58. }
  59. }
  60. public static String join(List<String>list, String split){
  61. StringBuilder sb = new StringBuilder();
  62. if(list != null && list.size() > 0){
  63. for(String str:list){
  64. if(StringUtils.isNotEmpty(str)){
  65. sb.append(str).append(split);
  66. }
  67. }
  68. if(sb.length()>0 && StringUtils.isNotEmpty(split)){
  69. sb.delete(sb.length() - split.length(), sb.length());
  70. }
  71. }
  72. return sb.toString();
  73. }
  74. public static Matcher matcher(String regex, String value) {
  75. Pattern pattern = Pattern.compile(regex);
  76. return pattern.matcher(value);
  77. }
  78. /**
  79. * 根据OSS文件路径获取文件输入流
  80. */
  81. public static InputStream getOSSInputStream(String urlStr) throws Exception {
  82. //获取OSS文件流
  83. URL imageUrl = new URL(urlStr);
  84. HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
  85. conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
  86. return conn.getInputStream();
  87. }
  88. /**
  89. * 获取字节数组
  90. */
  91. public static byte[] InputStreamToBytes(InputStream is) throws IOException {
  92. BufferedInputStream bis = new BufferedInputStream(is);
  93. ByteArrayOutputStream os = new ByteArrayOutputStream();
  94. int date = -1;
  95. while ((date = bis.read()) != -1) {
  96. os.write(date);
  97. }
  98. return os.toByteArray();
  99. }
  100. /**
  101. * 随机生成短信验证码
  102. * @param length 生成长度
  103. */
  104. public static String getCharAndNumber(int length) {
  105. StringBuilder val = new StringBuilder();
  106. Random random = new Random();
  107. for (int i = 0; i < length; i++) {
  108. String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
  109. if ("char".equalsIgnoreCase(charOrNum)) {
  110. int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
  111. val.append((char) (choice + random.nextInt(26)));
  112. } else {
  113. val.append(random.nextInt(10));
  114. }
  115. }
  116. return val.toString();
  117. }
  118. /**
  119. * 判断参数是否是数字
  120. * @param value 需要判断数据
  121. * @return 判断结果,数字则为true,反之false
  122. */
  123. public static boolean checkIsBigDecimal(Object value){
  124. try{
  125. if(value != null && StringUtils.isNotEmpty(String.valueOf(value))){
  126. new BigDecimal(String.valueOf(value));
  127. return true;
  128. } else {
  129. return false;
  130. }
  131. }catch (Exception e){
  132. return false;
  133. }
  134. }
  135. /**
  136. * 根据每页信息分组
  137. */
  138. public static <T> List<List<T>> getBatchSize(List<T> allIds, int size) {
  139. List<List<T>> batchIds = new ArrayList<>();
  140. if (allIds == null || allIds.size() == 0 || size <= 0) {
  141. return batchIds;
  142. }
  143. int i = 0;
  144. List<T> tmp = new ArrayList<>();
  145. for (T map : allIds) {
  146. tmp.add(map);
  147. i++;
  148. if (i % size == 0 || i == allIds.size()) {
  149. batchIds.add(tmp);
  150. tmp = new ArrayList<>();
  151. }
  152. }
  153. return batchIds;
  154. }
  155. /**
  156. * @param src
  157. * @throws IOException
  158. * @throws ClassNotFoundException
  159. */
  160. public static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException {
  161. ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
  162. ObjectOutputStream out = new ObjectOutputStream(byteOut);
  163. out.writeObject(src);
  164. ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
  165. ObjectInputStream in = new ObjectInputStream(byteIn);
  166. @SuppressWarnings("unchecked")
  167. List<T> dest = (List<T>) in.readObject();
  168. return dest;
  169. }
  170. /**
  171. * Description: Java8 Stream分割list集合
  172. * @param list 集合数据
  173. * @param splitSize 几个分割一组
  174. * @return 集合分割后的集合
  175. */
  176. public static <T> List<List<T>> splitList(List<T> list, int splitSize) {
  177. //判断集合是否为空
  178. if (CollectionUtils.isEmpty(list))
  179. return Collections.emptyList();
  180. //计算分割后的大小
  181. int maxSize = (list.size() + splitSize - 1) / splitSize;
  182. //开始分割
  183. return Stream.iterate(0, n -> n + 1)
  184. .limit(maxSize)
  185. .parallel()
  186. .map(a -> list.parallelStream().skip(a * splitSize).limit(splitSize).collect(Collectors.toList()))
  187. .filter(b -> !b.isEmpty())
  188. .collect(Collectors.toList());
  189. }
  190. }