tianyun 3 éve
szülő
commit
fcda7fc619

+ 10 - 0
springboot-main/pom.xml

@@ -21,6 +21,16 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.google.code.gson</groupId>
+            <artifactId>gson</artifactId>
+            <version>2.9.0</version>
+        </dependency>
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.6</version>
+        </dependency>
     </dependencies>
 
 </project>

+ 5 - 21
springboot-main/src/main/java/com/alvin/Application.java

@@ -1,29 +1,13 @@
 package com.alvin;
 
+import org.springframework.util.Assert;
 import org.springframework.util.StopWatch;
 
 public class Application {
     public static void main(String[] args) {
-        StopWatch watch = new StopWatch();
-        watch.start("no1");
-        for (int i = 0; i < 1000; i++) {
-            try {
-                System.out.println(123);
-            } catch (Exception e) {
-                System.out.println(123);
-            }
-        }
-        watch.stop();
-        watch.start("no2");
-        for (int i = 0; i < 10000; i++) {
-            try {
-                throw new RuntimeException("hhhh");
-            } catch (Exception e) {
-                System.out.println(123);
-            }
-        }
-        watch.stop();
-        System.out.println(watch.prettyPrint());
-
+        //Assert.isTrue("123".matches("^[a-zA-Z][0-9a-zA-Z_]*$"),"表名不符合规范,必须以英文开头,不能带有特殊字符");
+        Assert.isTrue("A1_23".matches("^[a-zA-Z][0-9a-zA-Z_]*$"),"表名不符合规范,必须以英文开头,不能带有特殊字符");
+        //Assert.isTrue("A1_*23".matches("^[a-zA-Z][0-9a-zA-Z_]*$"),"表名不符合规范,必须以英文开头,不能带有特殊字符");
+        Assert.isTrue("_A1_*23".matches("^[a-zA-Z][0-9a-zA-Z_]*$"),"表名不符合规范,必须以英文开头,不能带有特殊字符");
     }
 }

+ 67 - 0
springboot-main/src/main/java/com/alvin/util/CmdUtils.java

@@ -0,0 +1,67 @@
+package com.alvin.util;
+
+import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * cmd工具类
+ *
+ * @author tianyunperfect
+ */
+public class CmdUtils {
+
+    /**
+     * 执行并返回结果
+     *
+     * @param cmd
+     * @return
+     * @throws Exception
+     */
+    public static String execute(String cmd) throws Exception {
+        Process process = Runtime.getRuntime().exec(cmd);
+
+        //获取返回值
+        String line;
+        StringBuilder sb = new StringBuilder();
+        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "gbk"));
+        while ((line = br.readLine()) != null) {
+            System.out.println(line);
+            sb.append(line + "\n");
+        }
+        br.close();
+        return sb.toString();
+    }
+
+    /**
+     * 批量执行cmd,并返回结果,默认当前目录
+     * cd /home/test
+     * pwd
+     * rm -fr /home/proxy.log
+     *
+     * @param commands
+     * @return
+     */
+    public static List<String> executeNewFlow(List<String> commands) throws Exception {
+        List<String> rspList = new ArrayList<>();
+        Runtime run = Runtime.getRuntime();
+        Process proc = run.exec("/bin/bash", null, null);
+        BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
+        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
+        for (String line : commands) {
+            out.println(line);
+        }
+        // 这个命令必须执行,否则in流不结束。
+        out.println("exit");
+        String rspLine;
+        while ((rspLine = in.readLine()) != null) {
+            System.out.println(rspLine);
+            rspList.add(rspLine);
+        }
+        proc.waitFor();
+        in.close();
+        out.close();
+        proc.destroy();
+        return rspList;
+    }
+}

+ 102 - 0
springboot-main/src/main/java/com/alvin/util/DateUtil.java

@@ -0,0 +1,102 @@
+package com.alvin.util;
+
+import org.springframework.util.Assert;
+
+import java.time.*;
+import java.time.format.DateTimeFormatter;
+import java.time.temporal.TemporalAdjusters;
+import java.util.Date;
+
+/**
+ * 日期工具类
+ * 日期大小比较
+ * 日期加减
+ * 时间戳转换
+ *
+ * @author tianyunperfect
+ * @date 2020/05/28
+ */
+public class DateUtil {
+
+    /**
+     * 当前毫秒
+     *
+     * @return {@link Long}
+     */
+    public static Long getEpochMilli() {
+        return Instant.now().toEpochMilli();
+    }
+
+    /**
+     * 当前秒
+     *
+     * @return {@link Long}
+     */
+    public static Long getEpochSecond() {
+        return Instant.now().getEpochSecond();
+    }
+
+    /**
+     * 将Long类型的时间戳转换成String 类型的时间格式,时间格式为:yyyy-MM-dd HH:mm:ss
+     */
+    public static String convertTimeToString(Long time) {
+        Assert.notNull(time, "time is null");
+        DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+        return ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
+    }
+
+    /**
+     * 将字符串转日期成Long类型的时间戳,格式为:yyyy-MM-dd HH:mm:ss
+     */
+    public static Long convertTimeToLong(String time) {
+        Assert.notNull(time, "time is null");
+        DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+        LocalDateTime parse = LocalDateTime.parse("2018-05-29 13:52:50", ftf);
+        return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
+    }
+
+    /**
+     * 取本月第一天
+     */
+    public static LocalDate firstDayOfThisMonth() {
+        LocalDate today = LocalDate.now();
+        return today.with(TemporalAdjusters.firstDayOfMonth());
+    }
+
+    /**
+     * 取本月第N天
+     */
+    public static LocalDate dayOfThisMonth(int n) {
+        LocalDate today = LocalDate.now();
+        return today.withDayOfMonth(n);
+    }
+
+    /**
+     * 取本月最后一天
+     */
+    public static LocalDate lastDayOfThisMonth() {
+        LocalDate today = LocalDate.now();
+        return today.with(TemporalAdjusters.lastDayOfMonth());
+    }
+
+    /**
+     * 取本月第一天的开始时间
+     */
+    public static LocalDateTime startOfThisMonth() {
+        return LocalDateTime.of(firstDayOfThisMonth(), LocalTime.MIN);
+    }
+
+
+    /**
+     * 取本月最后一天的结束时间
+     */
+    public static LocalDateTime endOfThisMonth() {
+        return LocalDateTime.of(lastDayOfThisMonth(), LocalTime.MAX);
+    }
+
+    public static void main(String[] args) {
+        System.out.println(DateUtil.getEpochMilli());
+        System.out.println(DateUtil.getEpochSecond());
+    }
+
+}

+ 45 - 0
springboot-main/src/main/java/com/alvin/util/FileReader.java

@@ -0,0 +1,45 @@
+package com.alvin.util;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+
+public class FileReader {
+    private String nextLine;
+    private BufferedReader bufferedReader;
+
+    public FileReader(String filePath) throws IOException {
+        this(filePath, StandardCharsets.UTF_8.name());
+    }
+
+    public FileReader(String filePath, String encode) throws IOException {
+        this.bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), encode));
+        this.nextLine = this.bufferedReader.readLine();
+    }
+
+    public boolean hasNext() throws IOException {
+        if (nextLine == null) {
+            close();
+        }
+        return nextLine != null;
+    }
+
+    public String next() throws IOException {
+        String nowLine = nextLine;
+        nextLine = this.bufferedReader.readLine();
+        return nowLine;
+    }
+
+    public void close() throws IOException {
+        this.bufferedReader.close();
+    }
+
+    public static void main(String[] args) throws IOException {
+        FileReader fileReader = new FileReader("pom.xml");
+        while (fileReader.hasNext()) {
+            System.out.println(fileReader.next());
+        }
+    }
+}

+ 65 - 0
springboot-main/src/main/java/com/alvin/util/FileUtil.java

@@ -0,0 +1,65 @@
+package com.alvin.util;
+
+import java.io.*;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * 文件:
+ * 创建
+ * 是否存在
+ * 读取字符串
+ * 读取全部行
+ * 按行读取
+ * 删除
+ * <p>
+ * 文件夹:
+ * 是否为空
+ * 文件列表
+ * 删除文件夹
+ *
+ * @author mlamp
+ * @date 2022/05/21
+ */
+public class FileUtil {
+    public static final String DefaultEncode = "UTF-8";
+
+    public static String readToString(String filePath, String encode) throws UnsupportedEncodingException {
+        File file = new File(filePath);
+        Long fileLength = new File(filePath).length();
+        byte[] fileContent = new byte[fileLength.intValue()];
+        try {
+            FileInputStream in = new FileInputStream(file);
+            in.read(fileContent);
+            in.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return new String(fileContent, encode);
+    }
+
+    public static String readToString(String filePath) throws UnsupportedEncodingException {
+        return readToString(filePath, DefaultEncode);
+    }
+
+    public static List<String> readToLines(String filePath, String encode) throws UnsupportedEncodingException {
+        String[] strArr = readToString(filePath, encode).split("\\n");
+        ArrayList<String> strings = new ArrayList<>(strArr.length);
+        Collections.addAll(strings, strArr);
+        return strings;
+    }
+
+    public static List<String> readToLines(String filePath) throws UnsupportedEncodingException {
+        return readToLines(filePath, DefaultEncode);
+    }
+
+    public static void main(String[] args) throws UnsupportedEncodingException {
+        //System.out.println(FileUtil.readToString("pom.xml"));
+        System.out.println(readToLines("pom.xml").size());
+    }
+
+
+}

+ 95 - 0
springboot-main/src/main/java/com/alvin/util/JsonUtil.java

@@ -0,0 +1,95 @@
+package com.alvin.util;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.reflect.TypeToken;
+
+import java.util.List;
+import java.util.Map;
+
+
+/**
+ * json工具类
+ * <p>
+ * JsonUtil.getObject(str,JsonHello.class)
+ *
+ * @author tianyunperfect
+ * @date 2020/05/20
+ */
+public class JsonUtil {
+    private static final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
+
+    public JsonUtil() {
+    }
+
+    /**
+     * 转换为json字符串
+     *
+     * @param object 对象
+     * @return {@link String}
+     */
+    public static String toJsonStr(Object object) {
+        return gson.toJson(object);
+    }
+
+
+    /**
+     * 将json字符串 转换为 普通类
+     *
+     * @param jsonStr json str
+     * @param clazz   clazz
+     * @return {@link T}
+     */
+    public static <T> T getObject(String jsonStr, Class<T> clazz) {
+        return gson.fromJson(jsonStr, clazz);
+    }
+
+    /**
+     * 支持泛型等复杂类型
+     * new TypeToken<Result<List<Integer>>>(){}
+     * new TypeToken<List<Map<String, T>>>() {}
+     *
+     * @param jsonString json字符串
+     * @param typeToken  令牌类型
+     * @return {@link T}
+     */
+    public static <T> T getObject(String jsonString, TypeToken typeToken) {
+        return gson.fromJson(jsonString, typeToken.getType());
+    }
+
+    /**
+     * 转为数组
+     *
+     * @param jsonString
+     * @param tClass
+     * @param <T>
+     * @return
+     */
+    public static <T> T[] getArray(String jsonString, Class<T> tClass) {
+        return gson.fromJson(jsonString, TypeToken.getArray(tClass).getType());
+    }
+
+    /**
+     * 转为list
+     *
+     * @param jsonString
+     * @param tClass
+     * @param <T>
+     * @return
+     */
+    public static <T> List<T> getList(String jsonString, Class<T> tClass) {
+        return gson.fromJson(jsonString, new TypeToken<List<T>>() {
+        }.getType());
+    }
+
+    /**
+     * json字符串转成map的
+     *
+     * @param gsonString
+     * @return
+     */
+    public static <T> Map<String, T> getMap(String gsonString) {
+        return gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {
+        }.getType());
+    }
+}

+ 35 - 0
springboot-main/src/main/java/com/alvin/util/StringUtil.java

@@ -0,0 +1,35 @@
+package com.alvin.util;
+
+/**
+ * 字符串工具类
+ *
+ * @author mlamp
+ * @date 2022/05/21
+ */
+public class StringUtil {
+    /**
+     * 是否为空白字符串,比较常用
+     *
+     * @param s 年代
+     * @return {@link Boolean}
+     */
+    public static Boolean isBlank(String s) {
+        if (s != null && s.replaceAll("\\s*", "").length() > 0) {
+            return false;
+        }
+        return true;
+    }
+
+    public static Boolean isEmpty(String s) {
+        if (s != null && s.length() > 0) {
+            return false;
+        }
+        return true;
+    }
+
+    public static void main(String[] args) {
+        System.out.println(isBlank(null));
+        System.out.println(isBlank("           "));
+        System.out.println(isEmpty("           "));
+    }
+}

+ 1 - 2
test1/src/main/java/tmp.java

@@ -1,9 +1,8 @@
+import java.nio.file.Files;
 import java.util.BitSet;
 
 public class tmp {
     public static void main(String[] args) {
-        for (int i = 0; i < 10000; i++) {
 
-        }
     }
 }