* json中的“”转String对象时值为null diff --git a/src/main/java/com/yexuejc/base/converter/TimestampDeserializer.java b/src/main/java/com/yexuejc/base/converter/TimestampDeserializer.java index 53a2c50..4a2e8b7 100644 --- a/src/main/java/com/yexuejc/base/converter/TimestampDeserializer.java +++ b/src/main/java/com/yexuejc/base/converter/TimestampDeserializer.java @@ -1,17 +1,17 @@ package com.yexuejc.base.converter; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JsonDeserializer; -import com.yexuejc.base.constant.DateConsts; -import com.yexuejc.base.util.StrUtil; - import java.io.IOException; import java.sql.Timestamp; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Locale; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.yexuejc.base.constant.DateConsts; +import com.yexuejc.base.util.StrUtil; + /** * json转LocalDateTime * diff --git a/src/main/java/com/yexuejc/base/converter/TimestampSerializer.java b/src/main/java/com/yexuejc/base/converter/TimestampSerializer.java index d4262b2..a2e37f6 100644 --- a/src/main/java/com/yexuejc/base/converter/TimestampSerializer.java +++ b/src/main/java/com/yexuejc/base/converter/TimestampSerializer.java @@ -1,14 +1,14 @@ package com.yexuejc.base.converter; +import java.io.IOException; +import java.sql.Timestamp; +import java.text.SimpleDateFormat; + import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import com.yexuejc.base.constant.DateConsts; -import java.io.IOException; -import java.sql.Timestamp; -import java.text.SimpleDateFormat; - /** * Timestamp转json * diff --git a/src/main/java/com/yexuejc/base/pojo/CsvToBean.java b/src/main/java/com/yexuejc/base/pojo/CsvToBean.java index 457d2af..7787026 100644 --- a/src/main/java/com/yexuejc/base/pojo/CsvToBean.java +++ b/src/main/java/com/yexuejc/base/pojo/CsvToBean.java @@ -50,7 +50,7 @@ public class CsvToBean { return this; } - public boolean isHasHeader() { + public boolean hasHeader() { return hasHeader; } diff --git a/src/main/java/com/yexuejc/base/util/DateTimeUtil.java b/src/main/java/com/yexuejc/base/util/DateTimeUtil.java index 57dabbb..95924e4 100644 --- a/src/main/java/com/yexuejc/base/util/DateTimeUtil.java +++ b/src/main/java/com/yexuejc/base/util/DateTimeUtil.java @@ -448,6 +448,8 @@ public class DateTimeUtil { System.out.println(format(getYear4First().atTime(LocalTime.MIN))); System.out.println(format(getYear4Last().atTime(LocalTime.MAX))); + System.out.println(parseLocalDateTime10(System.currentTimeMillis() / 1000)); + System.out.println(parseLocalDateTime13(System.currentTimeMillis())); }*/ diff --git a/src/main/java/com/yexuejc/base/util/DateUtil.java b/src/main/java/com/yexuejc/base/util/DateUtil.java index 4a81ce7..5bdcdd3 100644 --- a/src/main/java/com/yexuejc/base/util/DateUtil.java +++ b/src/main/java/com/yexuejc/base/util/DateUtil.java @@ -59,8 +59,8 @@ public class DateUtil { /** * 比较两个日期大小 * - * @param date1 - * @param date2 + * @param date1 格式 yyyy-MM-dd + * @param date2 格式 yyyy-MM-dd * @return date1>date2返回1;date1=date2返回0;date10) { - digest.update(buffer, 0, len); - } - String sha1 = new BigInteger(1, digest.digest()).toString(16); - int length = 40 - sha1.length(); - if (length > 0) { - for (int i = 0; i < length; i++) { - sha1 = "0" + sha1; - } - } - return sha1; - } catch (NoSuchAlgorithmException e) { - logger.severe("system algorithm error."); - e.printStackTrace(); - } catch (FileNotFoundException e) { - logger.severe("file doesn't exist or is not a file"); - e.printStackTrace(); - } catch (IOException e) { - logger.severe("The operation file is an IO exception."); - e.printStackTrace(); - } finally { - try { - if (in != null) { - in.close(); - } - } catch (IOException e) { - logger.severe("close FileInputStream IO exception."); - } - } - return null; + return getDigest(file, "SHA-1"); } - /*** - * 计算SHA1码 - * - * @return String 适用于上G大的文件 - * @throws NoSuchAlgorithmException - * */ - public static String sha1ByBigFile(File file) { - MessageDigest messagedigest = null; - try { - messagedigest = MessageDigest.getInstance("SHA-1"); - FileInputStream in = new FileInputStream(file); - FileChannel ch = in.getChannel(); - MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length()); - messagedigest.update(byteBuffer); - return StrUtil.toHex(messagedigest.digest()); - } catch (NoSuchAlgorithmException e) { - logger.severe("system algorithm error."); - e.printStackTrace(); - } catch (FileNotFoundException e) { - logger.severe("file doesn't exist or is not a file"); - e.printStackTrace(); - } catch (IOException e) { - logger.severe("The operation file is an IO exception."); - e.printStackTrace(); - } - return null; - } /** * 文件md5 @@ -195,66 +150,40 @@ public class FileUtil { * @return */ public static String md5(File file) { - FileInputStream in = null; - try { - in = new FileInputStream(file); - MessageDigest digest = MessageDigest.getInstance("MD5"); - byte[] buffer = new byte[1024 * 1024 * 10]; - - int len = 0; - while ((len = in.read(buffer)) > 0) { - digest.update(buffer, 0, len); - } - String md5 = new BigInteger(1, digest.digest()).toString(16); - int length = 32 - md5.length(); - if (length > 0) { - for (int i = 0; i < length; i++) { - md5 = "0" + md5; - } - } - return md5; - } catch (IOException e) { - logger.severe("The operation file is an IO exception."); - } catch (NoSuchAlgorithmException e) { - logger.severe("system algorithm error."); - } finally { - try { - if (in != null) { - in.close(); - } - } catch (IOException e) { - logger.severe("close FileInputStream IO exception."); - } - } - return null; + return getDigest(file, "MD5"); } /** - * 对一个文件获取md5值 - * - * @return md5串 - * @throws NoSuchAlgorithmException + * 获取文件的散列值 + * @param file + * @param digestCode + * @return */ - public static String md5ByBigFile(File file) { - - MessageDigest messagedigest = null; - try { - messagedigest = MessageDigest.getInstance("MD5"); - FileInputStream in = new FileInputStream(file); - FileChannel ch = in.getChannel(); - MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, - file.length()); - messagedigest.update(byteBuffer); - return StrUtil.toHex(messagedigest.digest()); + private static String getDigest(File file, String digestCode) { + try (FileInputStream in = new FileInputStream(file)) { + MessageDigest digest = MessageDigest.getInstance(digestCode); + FileChannel channel = in.getChannel(); + ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024); // 1MB 缓冲区 + // 读取文件内容并更新 MessageDigest + while (channel.read(buffer) != -1) { + buffer.flip(); // 将 Buffer 从写模式切换到读模式 + digest.update(buffer); // 更新 MessageDigest + buffer.clear(); // 清空 Buffer + } + // 计算最终的 SHA-1 散列值 + byte[] sha1Bytes = digest.digest(); + // 将字节数组转换为十六进制字符串 + StringBuilder sha1Builder = new StringBuilder(); + for (byte b : sha1Bytes) { + sha1Builder.append(String.format("%02x", b)); + } + return sha1Builder.toString(); } catch (NoSuchAlgorithmException e) { - logger.severe("system algorithm error."); - e.printStackTrace(); + logger.log(Level.SEVERE, "system algorithm error.", e); } catch (FileNotFoundException e) { - logger.severe("file doesn't exist or is not a file"); - e.printStackTrace(); + logger.log(Level.SEVERE, "file doesn't exist or is not a file", e); } catch (IOException e) { - logger.severe("The operation file is an IO exception."); - e.printStackTrace(); + logger.log(Level.SEVERE, "The operation file is an IO exception.", e); } return null; } @@ -262,37 +191,24 @@ public class FileUtil { /** * 获取文件CRC32码 * - * @return String + * @return 获取失败返回-1 */ - public static String crc32(File file) { + public static long crc32(File file) { CRC32 crc32 = new CRC32(); // MessageDigest.get - FileInputStream fileInputStream = null; - try { - fileInputStream = new FileInputStream(file); - byte[] buffer = new byte[8192]; + try (FileInputStream fileInputStream = new FileInputStream(file);) { + byte[] buffer = new byte[1024 * 1024]; int length; while ((length = fileInputStream.read(buffer)) != -1) { crc32.update(buffer, 0, length); } - return crc32.getValue() + ""; + return crc32.getValue(); } catch (FileNotFoundException e) { - logger.severe("file doesn't exist or is not a file"); - e.printStackTrace(); - return null; + logger.log(Level.SEVERE, "file doesn't exist or is not a file", e); + return -1; } catch (IOException e) { - logger.severe("The operation file is an IO exception."); - e.printStackTrace(); - return null; - } finally { - try { - if (fileInputStream != null) { - fileInputStream.close(); - } - } catch (IOException e) { - logger.severe("close FileInputStream IO exception."); - e.printStackTrace(); - } + logger.log(Level.SEVERE, "The operation file is an IO exception.", e); + return -1; } } @@ -304,7 +220,7 @@ public class FileUtil { */ public static String base64ToStr(File file) { try { - byte[] bytes = Files.readAllBytes(Path.of(file.getPath())); + byte[] bytes = Files.readAllBytes(Paths.get(file.getPath())); return Base64.getEncoder().encodeToString(bytes); } catch (IOException e) { logger.severe("The operation file is an IO exception."); @@ -320,10 +236,10 @@ public class FileUtil { * * * @param decode {@link FileUtil#base64ToStr(File)} 的结果 - * @param fileName 保存文件名称(包含路径) + * @param fileName 文件名称(包含路径) * @return 返回保存地址 */ - public static String base64ToFile(String decode, String fileName) { + public static String base64ToFile(String decode, String fileName) throws IOException { return base64ToFile(Base64.getDecoder().decode(decode.getBytes()), fileName); } @@ -331,29 +247,16 @@ public class FileUtil { * base64转文件 * * - * 文件转base64请使用 {@link FileUtil#base64ToStr(File)} + * 文件转base64请使用 {@link FileUtil#base64ToStr(File)}} * * * @param decode baseByte * @param fileName 文件名称(包含路径) * @return 返回保存地址 */ - public static String base64ToFile(byte[] decode, String fileName) { - - FileOutputStream out = null; - try { - out = new FileOutputStream(fileName); + public static String base64ToFile(byte[] decode, String fileName) throws IOException { + try (FileOutputStream out = new FileOutputStream(fileName)) { out.write(decode); - } catch (IOException ioe) { - ioe.printStackTrace(); - } finally { - try { - if (out != null) { - out.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } } return fileName; } @@ -361,50 +264,18 @@ public class FileUtil { /** * 获取文件大小 :直接返回大小 * - * @param f + * @param path 文件地址 * @return f.length() */ - public static long size(File f) { - if (f.exists() && f.isFile()) { - return f.length(); + public static long size(Path path) throws IOException { + if (Files.exists(path) && Files.isRegularFile(path)) { + return Files.size(path); } else { logger.info("file doesn't exist or is not a file"); } return 0; } - /** - * 获取文件大小 : 用流的方式获取 - * - * @param f - * @return - */ - public static long size4Stream(File f) { - FileChannel fc = null; - try { - if (f.exists() && f.isFile()) { - FileInputStream fis = new FileInputStream(f); - fc = fis.getChannel(); - return fc.size(); - } else { - logger.info("file doesn't exist or is not a file"); - } - } catch (FileNotFoundException e) { - logger.severe("file doesn't exist or is not a file"); - } catch (IOException e) { - logger.severe("The operation file is an IO exception."); - } finally { - if (null != fc) { - try { - fc.close(); - } catch (IOException e) { - logger.severe("close FileInputStream IO exception."); - } - } - } - return 0; - } - /** * 字符串(csv格式)转 对象 * @@ -431,14 +302,15 @@ public class FileUtil { * * @param csvFilePath 文件地址 * @param cls 读取转化的对象 - * @param hasHeader 是否存在header + * @param header 解析列对应的java字段;用delimiter分割 + * @param hasHeader csv文件中第一行是否是header * @param delimiter 分隔符.默认【,】 * @param * @return */ - public static List readCsv(final String csvFilePath, Class cls, boolean hasHeader, char delimiter) { + public static List readCsv(final String csvFilePath, Class cls, boolean hasHeader, String header, char delimiter) { if (!isFileExist(csvFilePath)) { - throw new RuntimeException(String.format("解析用的csv:\u0020[%s] 文件不存在。", csvFilePath)); + throw new RuntimeException(String.format("解析用的csv: [%s] 文件不存在。", csvFilePath)); } if (StrUtil.isEmpty(delimiter)) { delimiter = ','; @@ -446,7 +318,12 @@ public class FileUtil { try { File csvFile = new File(csvFilePath); CsvMapper csvMapper = new CsvMapper(); - CsvSchema csvSchema = csvMapper.typedSchemaFor(cls).withStrictHeaders(hasHeader).withColumnSeparator(delimiter).withComments(); + CsvSchema.Builder builder = CsvSchema.builder(); + if (StrUtil.isNotEmpty(header)) { + builder.addColumns(Arrays.asList(header.split(String.valueOf(delimiter))), CsvSchema.ColumnType.STRING); + } + CsvSchema csvSchema = builder.build().withColumnSeparator(delimiter).withSkipFirstDataRow(hasHeader).withStrictHeaders(hasHeader).withComments(); + MappingIterator recordIterator = csvMapper.readerWithTypedSchemaFor(cls).with(csvSchema).readValues(csvFile); return recordIterator.readAll(); } catch (IOException e) { @@ -454,79 +331,60 @@ public class FileUtil { } } - /** - * 判断文件是否存在 - * - * @param filePath - * @return - */ - public static boolean isFileExist(String filePath) { - if (StrUtil.isEmpty(filePath)) { - return false; - } - - File file = new File(filePath); - return file.exists() && !file.isDirectory(); - } - /** * 分段读取大文件 * - * @param path 文件路径 + * @param csvFilePath 文件路径 * @param readFileBean 分段每次读取的bean 初始值需要设置每次读取的行数 * @param
读取结果类型bean * @return */ - public ReadFileBean readBigFile(String path, ReadFileBean readFileBean, Class readCls) throws FileNotFoundException { - File file = new File(path); - if (!file.exists() || file.isDirectory()) { - throw new FileNotFoundException("file:" + path + " is not found."); + public static ReadFileBean readBigFile(String csvFilePath, ReadFileBean readFileBean, Class readCls) throws IOException { + if (!isFileExist(csvFilePath)) { + throw new FileNotFoundException(String.format("解析用的csv: [%s] 文件不存在。", csvFilePath)); } - try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) { + if (!csvFilePath.endsWith(TYPE_CSV)) { + throw new IOException(String.format("解析用的csv: [%s] 文件不是CSV文件格式。", csvFilePath)); + } + List datas = new ArrayList<>(); + try (RandomAccessFile randomAccessFile = new RandomAccessFile(new File(csvFilePath), "r")) { if (readFileBean.getPointer() < 0) { readFileBean.setPointer(0); } randomAccessFile.seek(readFileBean.getPointer()); readFileBean.setFileLength(randomAccessFile.length()); - List datas = new ArrayList<>(); - int row = 1; + int row = 0; String line; while ((line = randomAccessFile.readLine()) != null && row <= readFileBean.getReadRowNum()) { row++; readFileBean.setPointer(randomAccessFile.getFilePointer()); datas.add(readFileBean.lineScavenge(charsetDecode(line, readFileBean.getReadCharset()))); } - - if (StrUtil.isEmpty(datas)) { - //无数据 - return readFileBean.setDatas(List.of()); - } - if (path.contains(TYPE_CSV)) { - //csv文件处理 - com.yexuejc.base.pojo.CsvToBean csvToBean = getCsvToBean(readCls); - readFileBean.setHeader(csvToBean.getHeader()); - if (csvToBean.isHasHeader()) { - //文件存在header,设置header优先,没设置使用文件的 - if (StrUtil.isNotEmpty(csvToBean.getHeader())) { - //替换header - datas.remove(0); - datas.add(0, csvToBean.getHeader()); - } else { - readFileBean.setHeader(datas.get(0)); - } - } else { - //文件不存在header,使用设置的 - datas.add(0, csvToBean.getHeader()); - } - - List dataList = readCsv(String.join("\n", datas), readCls, csvToBean.getDelimiter()); - readFileBean.setDatas(dataList); - } - } catch (FileNotFoundException e) { - logger.severe("file exists." + e.getMessage()); - } catch (IOException e) { - logger.severe("read file error." + e.getMessage()); } + if (StrUtil.isEmpty(datas)) { + //无数据 + return readFileBean.setDatas(new ArrayList<>()); + } + + //csv文件处理 + com.yexuejc.base.pojo.CsvToBean csvToBean = getCsvToBean(readCls); + readFileBean.setHeader(csvToBean.getHeader()); + if (csvToBean.hasHeader()) { + //文件存在header,设置header优先,没设置使用文件的 + if (StrUtil.isNotEmpty(csvToBean.getHeader())) { + //替换header + datas.remove(0); + datas.add(0, csvToBean.getHeader()); + } else { + readFileBean.setHeader(datas.get(0)); + } + } else { + //文件不存在header,使用设置的 + datas.add(0, csvToBean.getHeader()); + } + + List dataList = readCsv(String.join("\n", datas), readCls, csvToBean.getDelimiter()); + readFileBean.setDatas(dataList); return readFileBean; } @@ -539,10 +397,8 @@ public class FileUtil { */ public static com.yexuejc.base.pojo.CsvToBean getCsvToBean(Class cls) { CsvToBean annotation = cls.getAnnotation(CsvToBean.class); - Assert.notNull(annotation, cls.toString() + "类上需要添加注解@CsvToBean,并指定header。"); - com.yexuejc.base.pojo.CsvToBean csvToBean = new com.yexuejc.base.pojo.CsvToBean( - annotation.header(), annotation.delimiter(), annotation.hasHeader()); - return csvToBean; + Assert.notNull(annotation, cls + "类上需要添加注解@CsvToBean,并指定header。"); + return new com.yexuejc.base.pojo.CsvToBean(annotation.header(), annotation.delimiter(), annotation.hasHeader()); } /** diff --git a/src/main/java/com/yexuejc/base/util/StrUtil.java b/src/main/java/com/yexuejc/base/util/StrUtil.java index 81bbf6a..c6ffaad 100644 --- a/src/main/java/com/yexuejc/base/util/StrUtil.java +++ b/src/main/java/com/yexuejc/base/util/StrUtil.java @@ -1,7 +1,7 @@ package com.yexuejc.base.util; -import java.io.UnsupportedEncodingException; import java.lang.reflect.Array; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; @@ -29,7 +29,7 @@ public final class StrUtil { private StrUtil() { } - private static char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; /** * 判断字符串,数组,集合 是否为空(null,"",[],{}) @@ -79,12 +79,12 @@ public final class StrUtil { } else if (length < 1) { return ""; } else { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); for (int i = 0; i < length / 32; i++) { sb.append(genUUID()); } if (length % 32 > 0) { - sb.append(genUUID().substring(0, length % 32)); + sb.append(genUUID(), 0, length % 32); } return sb.toString(); } @@ -100,14 +100,14 @@ public final class StrUtil { */ public static String genNum() { int hashCode = UUID.randomUUID().toString().hashCode(); - StringBuffer num = new StringBuffer(); + StringBuilder num = new StringBuilder(); if (hashCode < 0) { - hashCode = 0 - hashCode; + hashCode = -hashCode; num.append("0"); } else { num.append("1"); } - return num.append(String.format("%010d", hashCode)).toString().substring(0, 8); + return num.append(String.format("%010d", hashCode)).substring(0, 8); } /** @@ -117,15 +117,15 @@ public final class StrUtil { * @return 转换后字符串 */ public static String toHex(byte[] buf) { - StringBuffer strbuf = new StringBuffer(buf.length * 2); + StringBuilder sb = new StringBuilder(buf.length * 2); int i; for (i = 0; i < buf.length; i++) { if (((int) buf[i] & 0xff) < 0x10) { - strbuf.append("0"); + sb.append("0"); } - strbuf.append(Long.toString((int) buf[i] & 0xff, 16)); + sb.append(Long.toString((int) buf[i] & 0xff, 16)); } - return strbuf.toString(); + return sb.toString(); } /** @@ -192,11 +192,7 @@ public final class StrUtil { */ public static String iso2utf(String str) { String utfStr = null; - try { - utfStr = new String(str.getBytes("ISO-8859-1"), "utf-8"); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } + utfStr = new String(str.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); return utfStr; } @@ -206,14 +202,11 @@ public final class StrUtil { * @param str * @return */ - private static Pattern pattern = Pattern.compile("[0-9]*"); + private static final Pattern pattern = Pattern.compile("[0-9]*"); public static boolean isNumeric(String str) { Matcher isNum = pattern.matcher(str); - if (!isNum.matches()) { - return false; - } - return true; + return isNum.matches(); } /** @@ -232,7 +225,7 @@ public final class StrUtil { for (int i = 0; i < 13; i++) { coded.append(HEX_CHAR[random.nextInt(16)]); } - coded.append(id.substring(0, 11)); + coded.append(id, 0, 11); for (int i = 0; i < 7; i++) { coded.append(HEX_CHAR[random.nextInt(16)]); } @@ -256,8 +249,8 @@ public final class StrUtil { } StringBuilder id = new StringBuilder(); - id.append(coded.substring(13, 24)); - id.append(coded.substring(31, 52)); + id.append(coded, 13, 24); + id.append(coded, 31, 52); return id.toString(); } @@ -277,7 +270,7 @@ public final class StrUtil { return null; } - Map map = new HashMap (16); + Map map = new HashMap<>(16); String[] kv = null; for (String entry : entrys) { if (isEmpty(entry)) { @@ -303,16 +296,15 @@ public final class StrUtil { * @return */ public static String getSignContent(Map sortedParams) { - StringBuffer content = new StringBuffer(); + StringBuilder content = new StringBuilder(); List keys = new ArrayList<>(sortedParams.keySet()); Collections.sort(keys); int index = 0; - for (int i = 0; i < keys.size(); ++i) { - String key = keys.get(i); + for (String key : keys) { Object value = sortedParams.get(key); if (isNotEmpty(key) && isNotEmpty(value)) { - content.append((index == 0 ? "" : "&") + key + "=" + value); + content.append(index == 0 ? "" : "&").append(key).append("=").append(value); ++index; } } @@ -343,8 +335,7 @@ public final class StrUtil { List keys = new ArrayList<>(sortedParams.keySet()); Collections.sort(keys); int index = 0; - for (int i = 0; i < keys.size(); ++i) { - String key = keys.get(i); + for (String key : keys) { Object value = sortedParams.get(key); map.put(key, value); ++index; @@ -475,8 +466,7 @@ public final class StrUtil { sb.append("Caused by: ").append(cClass).append(": ").append(eMessage).append(NEW_LINE); for (StackTraceElement element : stackTrace) { sb.append("\tat "); - sb.append(String.format(ERROR_MESSAGE_FORMAT, element.getClassName(), element.getMethodName(), - element.getFileName(), element.getLineNumber())); + sb.append(String.format(ERROR_MESSAGE_FORMAT, element.getClassName(), element.getMethodName(), element.getFileName(), element.getLineNumber())); sb.append(NEW_LINE); } return sb.toString(); diff --git a/src/main/java/com/yexuejc/base/util/SysUtil.java b/src/main/java/com/yexuejc/base/util/SysUtil.java index 83c3b66..b42044d 100644 --- a/src/main/java/com/yexuejc/base/util/SysUtil.java +++ b/src/main/java/com/yexuejc/base/util/SysUtil.java @@ -22,7 +22,7 @@ import java.util.logging.Logger; * @date: 2017/12/28 16:12 */ public class SysUtil { - private static Logger logger = Logger.getLogger(SysUtil.class.getName()); + private static final Logger logger = Logger.getLogger(SysUtil.class.getName()); private static final String PROJECT_ROOT_PATH = "java.io.tmpdir"; private SysUtil() { @@ -43,8 +43,8 @@ public class SysUtil { * @param clazz * @return */ - public static URL getRootPath(Class clazz, String filePath) { - return clazz.getClass().getResource(StrUtil.setStr(filePath, "/")); + public static URL getRootPath(Class> clazz, String filePath) { + return clazz.getResource(StrUtil.setStr(filePath, "/")); } /** @@ -73,9 +73,7 @@ public class SysUtil { 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue (1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy()); - singleThreadPool.execute(() -> { - threadRun.execute(); - }); + singleThreadPool.execute(threadRun::execute); singleThreadPool.shutdown(); } diff --git a/src/test/java/com/yexuejc/base/util/FileUtilTest.java b/src/test/java/com/yexuejc/base/util/FileUtilTest.java new file mode 100644 index 0000000..e376e46 --- /dev/null +++ b/src/test/java/com/yexuejc/base/util/FileUtilTest.java @@ -0,0 +1,72 @@ +package com.yexuejc.base.util; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.List; + +import com.yexuejc.base.pojo.ReadFileBean; +import com.yexuejc.base.util.bean.AppnodeCertCsvBean; + +/** + * + * @author: yexuejc + * @date: 2024/4/8 11:33 + */ +public class FileUtilTest { + public static void main(String[] args) throws IOException { + readCsvFile(); +// other(); + } + + private static void other() throws IOException { + System.out.println(FileUtil.getFileType("C:\\Users\\Administrator\\Desktop\\test.txt")); + boolean b = FileUtil.judeDirExists(new File("F:\\coding\\yexuejc-base2\\src\\test\\java\\com\\yexuejc\\base\\util\\test\\a")); + File file = new File("F:\\coding\\yexuejc-base2\\src\\test\\java\\com\\yexuejc\\base\\util\\test\\a\\test.txt"); + FileUtil.judeFileExists(file); + System.out.println("创建文件夹:" + b); + System.out.println("SHA1:" + FileUtil.sha1(file)); + //超大文件sha1 + long l = System.currentTimeMillis(); + System.out.println("SHA1:" + FileUtil.sha1(Paths.get("F:\\Docker\\win\\win10x64.iso").toFile()) + " 花费时间:" + (System.currentTimeMillis() - l)); + + System.out.println("MD5:" + FileUtil.md5(file)); + //超大文件MD5 + long l2 = System.currentTimeMillis(); + System.out.println("MD5:" + FileUtil.md5(Paths.get("F:\\Docker\\win\\win10x64.iso").toFile()) + " 花费时间:" + (System.currentTimeMillis() - l2)); + //超大文件MD5 + long l3 = System.currentTimeMillis(); + System.out.println("CRC32:" + FileUtil.crc32(Paths.get("F:\\Docker\\win\\win10x64.iso").toFile()) + " 花费时间:" + (System.currentTimeMillis() - l3)); + + String base64ToStr = FileUtil.base64ToStr(file); + System.out.println(base64ToStr); + String fileName = "F:\\coding\\yexuejc-base2\\src\\test\\java\\com\\yexuejc\\base\\util\\test\\a\\test2.txt"; + System.out.println(FileUtil.base64ToFile(base64ToStr, fileName)); + File file2 = Paths.get(fileName).toFile(); + System.out.println("SHA1:" + FileUtil.sha1(file2)); + System.out.println("MD5:" + FileUtil.md5(file2)); + + + System.out.println(FileUtil.size(file2.toPath())); + long l4 = System.currentTimeMillis(); + System.out.println(FileUtil.size(Paths.get("F:\\Docker\\win\\win10x64.iso")) + " 花费时间:" + (System.currentTimeMillis() - l4)); + + } + + private static void readCsvFile() throws IOException { + String path = "F:\\coding\\yexuejc-base2\\src\\test\\java\\com\\yexuejc\\base\\util\\test.csv"; + + List list = FileUtil.readCsv(path, AppnodeCertCsvBean.class, true, "enable,domain,protocol,deployHost,deployPath,uname,pwd,appnodeId", ','); + System.out.println("***********************************************"); + System.out.println(JsonUtil.formatPrinter(list)); + System.out.println("条数:" + list.size()); + + ReadFileBean readFileBean = new ReadFileBean<>(2); + do { + ReadFileBean bean = FileUtil.readBigFile(path, readFileBean, AppnodeCertCsvBean.class); + System.out.println("============================================"); + System.out.println(JsonUtil.formatPrinter(bean)); + } while (readFileBean.hasNext()); + + } +} diff --git a/src/test/java/com/yexuejc/base/util/SysUtilTest.java b/src/test/java/com/yexuejc/base/util/SysUtilTest.java new file mode 100644 index 0000000..f3cc470 --- /dev/null +++ b/src/test/java/com/yexuejc/base/util/SysUtilTest.java @@ -0,0 +1,22 @@ +package com.yexuejc.base.util; + +/** + * + * @author: yexuejc + * @date: 2024/4/8 11:22 + */ +public class SysUtilTest { + public static void main(String[] args) { + System.out.println(SysUtil.getCachePath()); + System.out.println(SysUtil.getRootPath(SysUtilTest.class, null)); + SysUtil.threadRun("test", () -> { + String threadName = Thread.currentThread().getName(); + System.out.println("当前线程的名称是:" + threadName); + }); + SysUtil.getThreadList().forEach(t -> { + System.out.println("线程名称:" + t.getName()); + }); + SysUtil.checkJvmMemory(); + System.out.println(SysUtil.jvmMemoryIsNotExecutable()); + } +} diff --git a/src/test/java/com/yexuejc/base/util/bean/AppnodeCertCsvBean.java b/src/test/java/com/yexuejc/base/util/bean/AppnodeCertCsvBean.java new file mode 100644 index 0000000..742509a --- /dev/null +++ b/src/test/java/com/yexuejc/base/util/bean/AppnodeCertCsvBean.java @@ -0,0 +1,105 @@ +package com.yexuejc.base.util.bean; + +import java.io.Serializable; + +import com.yexuejc.base.annotation.CsvToBean; + +/** + * + * @author: yexuejc + * @date: 2024/2/27 10:40 + */ +@CsvToBean(header = "enable,domain,protocol,deployHost,deployPath,uname,pwd,appnodeId", hasHeader = true) +public class AppnodeCertCsvBean implements Serializable { + /**是否生效:Y/N*/ + private String enable; + /**域名*/ + private String domain; + /** + * 部署协议 + * appnode + * ssh + * */ + private String protocol; + /** + * 部署服务器 + * 部署协议appnode: local 本机部署 + * 部署协议appnode: 域名 部署的远程appnode域名 + * 部署协议ssh : IP + * */ + private String deployHost; + /**部署证书位置*/ + private String deployPath; + /**服务器账号*/ + private String uname; + /**服务器密码*/ + private String pwd; + /** + * appnode协议时:且远程部署时,对应的远程appnode的ApiNodeId + */ + private Integer appnodeId; + + public String getEnable() { + return enable; + } + + public void setEnable(String enable) { + this.enable = enable; + } + + public String getDomain() { + return domain; + } + + public void setDomain(String domain) { + this.domain = domain; + } + + public String getProtocol() { + return protocol; + } + + public void setProtocol(String protocol) { + this.protocol = protocol; + } + + public String getDeployHost() { + return deployHost; + } + + public void setDeployHost(String deployHost) { + this.deployHost = deployHost; + } + + public String getDeployPath() { + return deployPath; + } + + public void setDeployPath(String deployPath) { + this.deployPath = deployPath; + } + + public String getUname() { + return uname; + } + + public void setUname(String uname) { + this.uname = uname; + } + + public String getPwd() { + return pwd; + } + + public void setPwd(String pwd) { + this.pwd = pwd; + } + + public Integer getAppnodeId() { + return appnodeId; + } + + public void setAppnodeId(Integer appnodeId) { + this.appnodeId = appnodeId; + } +}