123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- package org.springblade.common.utils;
- import cn.hutool.core.io.FileUtil;
- import org.apache.commons.lang.StringUtils;
- import org.springframework.util.CollectionUtils;
- import java.io.*;
- import java.math.BigDecimal;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
- /**
- * 通用工具类
- *
- * @author Chill
- */
- public class CommonUtil {
- public static Boolean checkBigDecimal(Object value){
- try{
- if(value != null && StringUtils.isNotEmpty(value.toString())){
- new BigDecimal(value.toString());
- return true;
- }
- }catch (Exception e){
- e.printStackTrace();
- }
- return false;
- }
- public static void removeFile(List<String> removeList){
- for(String fileUrl : removeList){
- try{
- FileUtil.del(new File(fileUrl));
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }
- public static String handleNull(Object obj) {
- if (null == obj) {
- return "";
- } else {
- return obj.toString().trim();
- }
- }
- public static String join(Object ...args){
- if(args!=null){
- if(args.length>2){
- List<String> list = Arrays.stream(args).limit(args.length - 1).map(CommonUtil::handleNull).collect(Collectors.toList());
- String split=handleNull(args[args.length-1]);
- return join(list, split);
- }else{
- return handleNull(args[0]);
- }
- }else {
- return "";
- }
- }
- public static String join(List<String>list, String split){
- StringBuilder sb = new StringBuilder();
- if(list != null && list.size() > 0){
- for(String str:list){
- if(StringUtils.isNotEmpty(str)){
- sb.append(str).append(split);
- }
- }
- if(sb.length()>0 && StringUtils.isNotEmpty(split)){
- sb.delete(sb.length() - split.length(), sb.length());
- }
- }
- return sb.toString();
- }
- public static Matcher matcher(String regex, String value) {
- Pattern pattern = Pattern.compile(regex);
- return pattern.matcher(value);
- }
- /**
- * 根据OSS文件路径获取文件输入流
- */
- public static InputStream getOSSInputStream(String urlStr) throws Exception {
- //获取OSS文件流
- URL imageUrl = new URL(urlStr);
- HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
- conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
- return conn.getInputStream();
- }
- /**
- * 获取字节数组
- */
- public static byte[] InputStreamToBytes(InputStream is) throws IOException {
- BufferedInputStream bis = new BufferedInputStream(is);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- int date = -1;
- while ((date = bis.read()) != -1) {
- os.write(date);
- }
- return os.toByteArray();
- }
- /**
- * 随机生成短信验证码
- * @param length 生成长度
- */
- public static String getCharAndNumber(int length) {
- StringBuilder val = new StringBuilder();
- Random random = new Random();
- for (int i = 0; i < length; i++) {
- String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
- if ("char".equalsIgnoreCase(charOrNum)) {
- int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
- val.append((char) (choice + random.nextInt(26)));
- } else {
- val.append(random.nextInt(10));
- }
- }
- return val.toString();
- }
- /**
- * 判断参数是否是数字
- * @param value 需要判断数据
- * @return 判断结果,数字则为true,反之false
- */
- public static boolean checkIsBigDecimal(Object value){
- try{
- if(value != null && StringUtils.isNotEmpty(String.valueOf(value))){
- new BigDecimal(String.valueOf(value));
- return true;
- } else {
- return false;
- }
- }catch (Exception e){
- return false;
- }
- }
- /**
- * 根据每页信息分组
- */
- public static <T> List<List<T>> getBatchSize(List<T> allIds, int size) {
- List<List<T>> batchIds = new ArrayList<>();
- if (allIds == null || allIds.size() == 0 || size <= 0) {
- return batchIds;
- }
- int i = 0;
- List<T> tmp = new ArrayList<>();
- for (T map : allIds) {
- tmp.add(map);
- i++;
- if (i % size == 0 || i == allIds.size()) {
- batchIds.add(tmp);
- tmp = new ArrayList<>();
- }
- }
- return batchIds;
- }
- /**
- * @param src
- * @throws IOException
- * @throws ClassNotFoundException
- */
- public static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException {
- ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
- ObjectOutputStream out = new ObjectOutputStream(byteOut);
- out.writeObject(src);
- ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
- ObjectInputStream in = new ObjectInputStream(byteIn);
- @SuppressWarnings("unchecked")
- List<T> dest = (List<T>) in.readObject();
- return dest;
- }
- /**
- * Description: Java8 Stream分割list集合
- * @param list 集合数据
- * @param splitSize 几个分割一组
- * @return 集合分割后的集合
- */
- public static <T> List<List<T>> splitList(List<T> list, int splitSize) {
- //判断集合是否为空
- if (CollectionUtils.isEmpty(list))
- return Collections.emptyList();
- //计算分割后的大小
- int maxSize = (list.size() + splitSize - 1) / splitSize;
- //开始分割
- return Stream.iterate(0, n -> n + 1)
- .limit(maxSize)
- .parallel()
- .map(a -> list.parallelStream().skip(a * splitSize).limit(splitSize).collect(Collectors.toList()))
- .filter(b -> !b.isEmpty())
- .collect(Collectors.toList());
- }
- }
|