From 559fa5a1208fa29a35d703bc8de1a838921ea744 Mon Sep 17 00:00:00 2001 From: VIVIMAN Date: Wed, 20 Apr 2022 18:05:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ++ src/main/java/com/insigma/utils/FileUtil.java | 6 +- .../com/insigma/utils/LinuxCommandUtil.java | 1 + .../insigma/utils/MessageSourceHandler.java | 48 ----------- .../com/insigma/utils/WinCommandUtil.java | 20 +---- src/main/java/com/insigma/utils/ZipUtil.java | 80 ++++++++++--------- .../java/com/insigma/utils/DbUtilTest.java | 16 ++++ .../java/com/insigma/utils/FileUtilTest.java | 55 +++++++++++++ .../com/insigma/utils/WinCommandUtilTest.java | 23 ++++++ .../java/com/insigma/utils/ZipUtilTest.java | 38 +++++++++ src/test/java/com/insigma/utils/my.ini | 79 ++++++++++++++++++ 11 files changed, 268 insertions(+), 104 deletions(-) delete mode 100644 src/main/java/com/insigma/utils/MessageSourceHandler.java create mode 100644 src/test/java/com/insigma/utils/DbUtilTest.java create mode 100644 src/test/java/com/insigma/utils/FileUtilTest.java create mode 100644 src/test/java/com/insigma/utils/WinCommandUtilTest.java create mode 100644 src/test/java/com/insigma/utils/ZipUtilTest.java create mode 100644 src/test/java/com/insigma/utils/my.ini diff --git a/pom.xml b/pom.xml index a6827a7..9da3c38 100644 --- a/pom.xml +++ b/pom.xml @@ -181,6 +181,12 @@ hutool-all 5.1.0 + + junit + junit + 4.12 + test + diff --git a/src/main/java/com/insigma/utils/FileUtil.java b/src/main/java/com/insigma/utils/FileUtil.java index 93a2ba3..a1356d2 100644 --- a/src/main/java/com/insigma/utils/FileUtil.java +++ b/src/main/java/com/insigma/utils/FileUtil.java @@ -68,8 +68,10 @@ public class FileUtil { temp.delete(); } if (temp.isDirectory()) { - delAllFile(path + File.separatorChar + tempList[i]);// 先删除文件夹里面的文件 - File folderPath = new File(path + File.separatorChar + tempList[i]); // 再删除空文件夹 + // 先删除文件夹里面的文件 + delAllFile(path + File.separatorChar + tempList[i]); + // 再删除空文件夹 + File folderPath = new File(path + File.separatorChar + tempList[i]); folderPath.delete(); } } diff --git a/src/main/java/com/insigma/utils/LinuxCommandUtil.java b/src/main/java/com/insigma/utils/LinuxCommandUtil.java index 2ae1607..45bcfca 100644 --- a/src/main/java/com/insigma/utils/LinuxCommandUtil.java +++ b/src/main/java/com/insigma/utils/LinuxCommandUtil.java @@ -36,6 +36,7 @@ public class LinuxCommandUtil implements Runnable { exitVal = process.waitFor(); } catch (IOException | InterruptedException e) { + log.error("执行命令发生异常:{}", e.getMessage()); e.printStackTrace(); } diff --git a/src/main/java/com/insigma/utils/MessageSourceHandler.java b/src/main/java/com/insigma/utils/MessageSourceHandler.java deleted file mode 100644 index 279bb17..0000000 --- a/src/main/java/com/insigma/utils/MessageSourceHandler.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.insigma.utils; - - -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.MessageSource; -import org.springframework.context.i18n.LocaleContextHolder; -import org.springframework.stereotype.Component; -import org.springframework.web.servlet.support.RequestContextUtils; - -import javax.servlet.http.HttpServletRequest; - -/** - * @author Vivim - */ -@Component -@Slf4j -public class MessageSourceHandler { - // https://blog.csdn.net/P397226804/article/details/103960592 - @Autowired - private HttpServletRequest request; - - @Autowired - private MessageSource messageSource; - - /** - * 如果是根据Request请求的语言来决定国际化: - * - * @param messageKey - * @return - */ - public String getMessage(String messageKey) { - String message = messageSource.getMessage(messageKey, null, RequestContextUtils.getLocale(request)); - return message; - } - - /** - * 如果是根据应用部署的服务器系统来决定国际化: - * - * @param messageKey - * @return - */ - public String getMessageServer(String messageKey) { - String message = messageSource.getMessage(messageKey, null, LocaleContextHolder.getLocale()); - return message; - } - -} diff --git a/src/main/java/com/insigma/utils/WinCommandUtil.java b/src/main/java/com/insigma/utils/WinCommandUtil.java index 72daba0..ea4ca2d 100644 --- a/src/main/java/com/insigma/utils/WinCommandUtil.java +++ b/src/main/java/com/insigma/utils/WinCommandUtil.java @@ -4,7 +4,6 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.util.StringUtils; import java.io.*; -import java.util.prefs.Preferences; /** * CommandImpUtil @@ -17,7 +16,7 @@ public class WinCommandUtil implements Runnable { private String command; private final String CMD = "cmd"; - private final String EXE = "cmd.exe /c "; + private final String EXE = "cmd /c "; public WinCommandUtil(String command) { if(StringUtils.startsWithIgnoreCase(command, CMD)){ this.command = command; @@ -40,11 +39,11 @@ public class WinCommandUtil implements Runnable { exitVal = process.waitFor(); } catch (IOException | InterruptedException e) { + log.error("执行命令发生异常:{}", e.getMessage()); e.printStackTrace(); } if (exitVal != 0) { - log.error("执行命令发生异常:{}", exitVal); throw new RuntimeException("cmd任务执行失败"); } } @@ -75,20 +74,7 @@ public class WinCommandUtil implements Runnable { } public static class RegisterUtil { + // TODO 无法绕开系统权限 - String[] keys = {"version", "initial", "creator"}; - String[] values = {"1.3", "ini.mp3", "caokai1818@sina.com"}; - //把相应的值储存到变量中去 - public void writeValue() { - // HKEY_LOCAL_MACHINE\Software\JavaSoft\prefs下写入注册表值. - Preferences pre = Preferences.systemRoot().node("/javaplayer"); - for (int i = 0; i < keys.length; i++) { - pre.put(keys[i], values[i]); - } - } - public static void main(String[] args) { - RegisterUtil reg = new RegisterUtil(); - reg.writeValue(); - } } } \ No newline at end of file diff --git a/src/main/java/com/insigma/utils/ZipUtil.java b/src/main/java/com/insigma/utils/ZipUtil.java index 7e83f0c..afc3a66 100644 --- a/src/main/java/com/insigma/utils/ZipUtil.java +++ b/src/main/java/com/insigma/utils/ZipUtil.java @@ -14,56 +14,62 @@ public class ZipUtil { private static final String SX = "/"; private static final String XX = "\\"; public static void zip7Z(String dirName, String zipFileName) { - if (XX.equals(File.separator)) { - String cmd7z = getRootPath() + "7-Zip/7z.exe"; - try { - String cmd = "cmd.exe /c " + cmd7z.trim().replace(" ", "\" \"") + " a -tzip \"" + zipFileName + "\" \"" + dirName + "\\*\""; - new WinCommandUtil(cmd).run(); - } catch (Exception ie) { - ie.printStackTrace(); - } - } else if (SX.equals(File.separator)) { - try { - String cmd777 = "chmod -R 777 "+dirName; - new LinuxCommandUtil(cmd777).run(); + File file = new File(dirName); + if(file.exists()){ + if (XX.equals(File.separator)) { + String cmd7z = getRootPath() + "7-Zip/7z.exe"; + try { + String cmd = "cmd.exe /c " + cmd7z.trim().replace(" ", "\" \"") + " a -tzip \"" + zipFileName + "\" \"" + dirName + "\\*\""; + new WinCommandUtil(cmd).run(); + } catch (Exception ie) { + ie.printStackTrace(); + } + } else if (SX.equals(File.separator)) { + try { + String cmd777 = "chmod -R 777 "+dirName; + new LinuxCommandUtil(cmd777).run(); - int index = cmd777.lastIndexOf(File.separator); - String cmd777Short = cmd777.substring(0,index); - new LinuxCommandUtil(cmd777Short).run(); + int index = cmd777.lastIndexOf(File.separator); + String cmd777Short = cmd777.substring(0,index); + new LinuxCommandUtil(cmd777Short).run(); - String cmd = "7z a "+zipFileName+" "+dirName+" "; - new LinuxCommandUtil(cmd).run(); - } catch (Exception ie) { - ie.printStackTrace(); + String cmd = "7z a "+zipFileName+" "+dirName+" "; + new LinuxCommandUtil(cmd).run(); + } catch (Exception ie) { + ie.printStackTrace(); + } } } } public static void unZip7Z(String filepath, String destinationDir) { - if (XX.equals(File.separator)) { - String cmd7z = getRootPath() + "7-Zip/7z.exe"; - try { - String cmd = "cmd.exe /c " + cmd7z.trim().replace(" ", "\" \"") + " x \"" + filepath + "\" -o\"" + destinationDir + "\" " + " -y "; - new WinCommandUtil(cmd).run(); - } catch (Exception ie) { - ie.printStackTrace(); - } - } else if (SX.equals(File.separator)) { - try { + File file = new File(filepath); + if(file.exists()){ + if (XX.equals(File.separator)) { + String cmd7z = getRootPath() + "7-Zip/7z.exe"; + try { + String cmd = "cmd.exe /c " + cmd7z.trim().replace(" ", "\" \"") + " x \"" + filepath + "\" -o\"" + destinationDir + "\" " + " -y "; + new WinCommandUtil(cmd).run(); + } catch (Exception ie) { + ie.printStackTrace(); + } + } else if (SX.equals(File.separator)) { + try { - String cmd777 ="chmod -R 777 "+filepath+File.separator; - new WinCommandUtil(cmd777).run(); + String cmd777 ="chmod -R 777 "+filepath+File.separator; + new WinCommandUtil(cmd777).run(); - String cmd = "unzip -o "+filepath+" -d "+destinationDir+""; - new WinCommandUtil(cmd).run(); + String cmd = "unzip -o "+filepath+" -d "+destinationDir+""; + new WinCommandUtil(cmd).run(); - new WinCommandUtil(cmd777).run(); - } catch (Exception ie) { - ie.printStackTrace(); + new WinCommandUtil(cmd777).run(); + } catch (Exception ie) { + ie.printStackTrace(); + } } } } - public static String getRootPath() { + private static String getRootPath() { return AppCfg.HZB + "/tomcat8/webapps/qggwy/WEB-INF/classes/static/softTools/"; } } diff --git a/src/test/java/com/insigma/utils/DbUtilTest.java b/src/test/java/com/insigma/utils/DbUtilTest.java new file mode 100644 index 0000000..e13bdce --- /dev/null +++ b/src/test/java/com/insigma/utils/DbUtilTest.java @@ -0,0 +1,16 @@ +package com.insigma.utils; + +import cn.hutool.db.Session; +import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; +import java.sql.SQLException; + +public class DbUtilTest { + + @Test + public void getSessionTest() throws SQLException { + Session session = DbUtil.getSession("jdbc:mysql://127.0.0.1:35017/hy_qggwy", "root", "admin"); + String queryString = session.queryString("select count(1) from a01 limit 1"); + assertNotEquals("0", queryString); + } +} \ No newline at end of file diff --git a/src/test/java/com/insigma/utils/FileUtilTest.java b/src/test/java/com/insigma/utils/FileUtilTest.java new file mode 100644 index 0000000..901bbc0 --- /dev/null +++ b/src/test/java/com/insigma/utils/FileUtilTest.java @@ -0,0 +1,55 @@ +package com.insigma.utils; + + +import lombok.extern.slf4j.Slf4j; +import org.junit.Test; + +import java.io.*; + +import static org.junit.jupiter.api.Assertions.*; + +@Slf4j +public class FileUtilTest { + + @Test + public void replaceLine() throws IOException { + String path = "D:\\EPWork\\idea\\java_tool\\src\\test\\java\\com\\insigma\\utils\\my.ini"; + String key = "read_rnd_buffer_size"; + String val = "read_rnd_buffer_size = 64M"; + FileUtil.replaceLine(path, key, val); + String temp = getValByKey(path, key); + assertEquals(val, temp); + log.info("完成测试!"); + } + + /*** + * 读取某行数据 + * @param path + * @param key + * @return + * @throws IOException + */ + private String getValByKey(String path, String key) throws IOException { + File file = new File(path); + FileInputStream fis = new FileInputStream(file); + InputStreamReader isr = new InputStreamReader(fis); + BufferedReader br = new BufferedReader(isr); + String temp; + // 保存该行前面的内容 + while ((temp = br.readLine()) != null) { + if (temp.trim().startsWith(key)) { + return temp; + } + } + return null; + } + + @Test + public void delAllFile() { + String path = "D:\\hzb2021\\mysql\\data-jyb"; + FileUtil.delAllFile(path); + File file = new File(path); + assertFalse(file.exists()); + log.info("完成测试!"); + } +} \ No newline at end of file diff --git a/src/test/java/com/insigma/utils/WinCommandUtilTest.java b/src/test/java/com/insigma/utils/WinCommandUtilTest.java new file mode 100644 index 0000000..5980def --- /dev/null +++ b/src/test/java/com/insigma/utils/WinCommandUtilTest.java @@ -0,0 +1,23 @@ +package com.insigma.utils; + + +import lombok.extern.slf4j.Slf4j; +import org.junit.Test; + +import java.io.File; + +import static org.junit.jupiter.api.Assertions.*; + +@Slf4j +public class WinCommandUtilTest { + + @Test + public void run() { + // 注意:该部分需要注意使用的分割线 下划线- + String dir = "e:\\ccc"; + new WinCommandUtil("mkdir " + dir).run(); + File file = new File(dir); + assertTrue(file.exists()); + log.info("测试完成!"); + } +} \ No newline at end of file diff --git a/src/test/java/com/insigma/utils/ZipUtilTest.java b/src/test/java/com/insigma/utils/ZipUtilTest.java new file mode 100644 index 0000000..9feb5fc --- /dev/null +++ b/src/test/java/com/insigma/utils/ZipUtilTest.java @@ -0,0 +1,38 @@ +package com.insigma.utils; + + +import com.insigma.config.AppCfg; +import lombok.extern.slf4j.Slf4j; +import org.junit.Test; + +import java.io.File; + +import static org.junit.jupiter.api.Assertions.*; + +@Slf4j +public class ZipUtilTest { + + static { + AppCfg.HZB = "D:\\hzb2021"; + } + + @Test + public void zip7Z() { + String dir = "E:\\aaa"; + String zip = "E:\\aaa.zip"; + ZipUtil.zip7Z(dir, zip); + File file = new File(zip); + assertTrue(file.exists()); + log.info("测试完成!"); + } + + @Test + public void unZip7Z() { + String dir = "E:\\bbb"; + String zip = "E:\\aaa.zip"; + ZipUtil.unZip7Z(zip, dir); + File file = new File(dir); + assertTrue(file.exists()); + log.info("测试完成!"); + } +} \ No newline at end of file diff --git a/src/test/java/com/insigma/utils/my.ini b/src/test/java/com/insigma/utils/my.ini new file mode 100644 index 0000000..b99cfc5 --- /dev/null +++ b/src/test/java/com/insigma/utils/my.ini @@ -0,0 +1,79 @@ +#high +[client] +port = 35017 +socket = MySQL + +[mysqld] +port = 35017 +socket = MySQL +max_allowed_packet = 64M +key_buffer_size = 32M +table_open_cache = 2048 +sort_buffer_size = 32M +read_buffer_size = 8M +read_rnd_buffer_size = 64M +myisam_sort_buffer_size = 64M +net_buffer_length = 1M +thread_cache_size = 16 +thread_stack = 16M +query_cache_size = 256M +# Try number of CPU's*2 for thread_concurrency +thread_concurrency = 8 +log_bin_trust_function_creators=1 +server-id = 1 +character-set-server=utf8 +default-storage-engine=INNODB +innodb_buffer_pool_size=3000M +innodb_additional_mem_pool_size=64M +innodb_flush_log_at_trx_commit =0 +innodb_log_buffer_size=32M +innodb_log_file_size=128M +sync_binlog = 1000 +innodb_autoextend_increment=32M +innodb_stats_on_metadata=OFF +innodb_read_io_threads=4 +innodb_write_io_threads = 4 +innodb_file_per_table = ON +innodb_file_per_table=1 +tmp_table_size = 256M + +max_connections = 500 +max_connect_errors = 30 +binlog_cache_size = 4M +max_heap_table_size = 128M +join_buffer_size = 32M + +sort_buffer_size = 12M + +[mysqldump] +quick +max_allowed_packet = 64M +key_buffer_size=128M +tmp_table_size =128M + + +[myisamchk] +read_buffer = 2M +write_buffer = 2M + +[mysql] +no-auto-rehash +default-character-set=utf8 + +[mysqlhotcopy] +interactive-timeout +max_allowed_packet = 64M +thread_cache_size = 16 +thread_concurrency = 8 +#table_cache = 4096 5.1.3 table_open_cache +#memlock +tmp_table_size = 128M +transaction_isolation = REPEATABLE-READ +#external-locking +#skip-networking +#log_slave_updates +#log +#log_warnings +#log_slow_queries +#long_query_time = 6 +#log_long_format