From a49bd6d127aed39c2f116eea9e4af10f6b8d6913 Mon Sep 17 00:00:00 2001 From: Tsln Date: Thu, 4 Mar 2021 21:53:37 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=AE=97=E6=9C=AF?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E9=AA=8C=E8=AF=81=E7=A0=81=E5=9C=A8=20Java?= =?UTF-8?q?=2015+=20=E7=9A=84=E5=85=BC=E5=AE=B9=E9=97=AE=E9=A2=98=20(#607)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit cdbc3a8faaf5958489758608329aa70095b37bb3) --- .../security/config/bean/LoginProperties.java | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/security/config/bean/LoginProperties.java b/eladmin-system/src/main/java/me/zhengjie/modules/security/config/bean/LoginProperties.java index ca57213a..b13f173e 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/security/config/bean/LoginProperties.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/security/config/bean/LoginProperties.java @@ -80,7 +80,7 @@ public class LoginProperties { switch (loginCode.getCodeType()) { case arithmetic: // 算术类型 https://gitee.com/whvse/EasyCaptcha - captcha = new ArithmeticCaptcha(loginCode.getWidth(), loginCode.getHeight()); + captcha = new FixedArithmeticCaptcha(loginCode.getWidth(), loginCode.getHeight()); // 几位数运算,默认是两位 captcha.setLen(loginCode.getLength()); break; @@ -109,4 +109,27 @@ public class LoginProperties { } return captcha; } + + static class FixedArithmeticCaptcha extends ArithmeticCaptcha { + public FixedArithmeticCaptcha(int width, int height) { + super(width, height); + } + + @Override + protected char[] alphas() { + // 生成随机数字和运算符 + int n1 = num(1, 10), n2 = num(1, 10); + int opt = num(3); + + // 计算结果 + int res = new int[]{n1 + n2, n1 - n2, n1 * n2}[opt]; + // 转换为字符运算符 + char optChar = "+-x".charAt(opt); + + this.setArithmeticString(String.format("%s%c%s=?", n1, optChar, n2)); + this.chars = String.valueOf(res); + + return chars.toCharArray(); + } + } } From cd85594e48502cfd1b175b97a10e1ac99a1549d7 Mon Sep 17 00:00:00 2001 From: Zheng Jie <201507802@qq.com> Date: Fri, 5 Mar 2021 11:26:08 +0800 Subject: [PATCH 2/3] =?UTF-8?q?[=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96](v2.6?= =?UTF-8?q?)=EF=BC=9A=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/me/zhengjie/utils/CloseUtil.java | 47 ++++++ .../main/java/me/zhengjie/utils/FileUtil.java | 37 ++-- .../zhengjie/modules/mnt/util/SqlUtils.java | 39 ++--- .../zhengjie/modules/mnt/util/ZipUtils.java | 158 ------------------ .../service/impl/MonitorServiceImpl.java | 7 +- 5 files changed, 94 insertions(+), 194 deletions(-) create mode 100644 eladmin-common/src/main/java/me/zhengjie/utils/CloseUtil.java delete mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/ZipUtils.java diff --git a/eladmin-common/src/main/java/me/zhengjie/utils/CloseUtil.java b/eladmin-common/src/main/java/me/zhengjie/utils/CloseUtil.java new file mode 100644 index 00000000..98d375be --- /dev/null +++ b/eladmin-common/src/main/java/me/zhengjie/utils/CloseUtil.java @@ -0,0 +1,47 @@ +/* + * Copyright 2019-2020 Zheng Jie + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package me.zhengjie.utils; + +import java.io.Closeable; + +/** + * @author Zheng Jie + * @website https://el-admin.vip + * @description 用于关闭各种连接,缺啥补啥 + * @date 2021-03-05 + **/ +public class CloseUtil { + + public static void close(Closeable closeable) { + if (null != closeable) { + try { + closeable.close(); + } catch (Exception e) { + // 静默关闭 + } + } + } + + public static void close(AutoCloseable closeable) { + if (null != closeable) { + try { + closeable.close(); + } catch (Exception e) { + // 静默关闭 + } + } + } +} diff --git a/eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java b/eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java index bf23eff9..3ac841d1 100644 --- a/eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java +++ b/eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java @@ -153,20 +153,26 @@ public class FileUtil extends cn.hutool.core.io.FileUtil { /** * inputStream 转 File */ - static File inputStreamToFile(InputStream ins, String name) throws Exception { + static File inputStreamToFile(InputStream ins, String name){ File file = new File(SYS_TEM_DIR + name); if (file.exists()) { return file; } - OutputStream os = new FileOutputStream(file); - int bytesRead; - int len = 8192; - byte[] buffer = new byte[len]; - while ((bytesRead = ins.read(buffer, 0, len)) != -1) { - os.write(buffer, 0, bytesRead); + OutputStream os = null; + try { + os = new FileOutputStream(file); + int bytesRead; + int len = 8192; + byte[] buffer = new byte[len]; + while ((bytesRead = ins.read(buffer, 0, len)) != -1) { + os.write(buffer, 0, bytesRead); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + CloseUtil.close(os); + CloseUtil.close(ins); } - os.close(); - ins.close(); return file; } @@ -257,7 +263,10 @@ public class FileUtil extends cn.hutool.core.io.FileUtil { public static boolean check(File file1, File file2) { String img1Md5 = getMd5(file1); String img2Md5 = getMd5(file2); - return img1Md5.equals(img2Md5); + if(img1Md5 != null){ + return img1Md5.equals(img2Md5); + } + return false; } /** @@ -270,16 +279,19 @@ public class FileUtil extends cn.hutool.core.io.FileUtil { private static byte[] getByte(File file) { // 得到文件长度 byte[] b = new byte[(int) file.length()]; + InputStream in = null; try { - InputStream in = new FileInputStream(file); + in = new FileInputStream(file); try { System.out.println(in.read(b)); } catch (IOException e) { log.error(e.getMessage(), e); } - } catch (FileNotFoundException e) { + } catch (Exception e) { log.error(e.getMessage(), e); return null; + } finally { + CloseUtil.close(in); } return b; } @@ -341,5 +353,4 @@ public class FileUtil extends cn.hutool.core.io.FileUtil { public static String getMd5(File file) { return getMd5(getByte(file)); } - } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/SqlUtils.java b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/SqlUtils.java index 26734ef1..d7e06b17 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/SqlUtils.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/SqlUtils.java @@ -19,7 +19,7 @@ import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.util.StringUtils; import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; - +import me.zhengjie.utils.CloseUtil; import javax.sql.DataSource; import java.io.BufferedReader; import java.io.File; @@ -35,9 +35,6 @@ import java.util.List; @Slf4j public class SqlUtils { - public static final String COLON = ":"; - - /** * 获取数据源 * @@ -102,6 +99,8 @@ public class SqlUtils { } catch (Exception e) { log.error("create connection error, jdbcUrl: {}", jdbcUrl); throw new RuntimeException("create connection error, jdbcUrl: " + jdbcUrl); + } finally { + CloseUtil.close(connection); } return connection; } @@ -117,17 +116,6 @@ public class SqlUtils { } } - - public static void closeResult(ResultSet rs) { - if (rs != null) { - try { - rs.close(); - } catch (Exception e) { - log.error(e.getMessage(),e); - } - } - } - public static boolean testConnection(String jdbcUrl, String userName, String password) { Connection connection = null; try { @@ -162,15 +150,22 @@ public class SqlUtils { * @param connection / * @param sqlList / */ - public static void batchExecute(Connection connection, List sqlList) throws SQLException { - Statement st = connection.createStatement(); - for (String sql : sqlList) { - if (sql.endsWith(";")) { - sql = sql.substring(0, sql.length() - 1); + public static void batchExecute(Connection connection, List sqlList) { + Statement st = null; + try { + st = connection.createStatement(); + for (String sql : sqlList) { + if (sql.endsWith(";")) { + sql = sql.substring(0, sql.length() - 1); + } + st.addBatch(sql); } - st.addBatch(sql); + st.executeBatch(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } finally { + CloseUtil.close(st); } - st.executeBatch(); } /** diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/ZipUtils.java b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/ZipUtils.java deleted file mode 100644 index 2bf4f34c..00000000 --- a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/ZipUtils.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Copyright 2019-2020 Zheng Jie - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package me.zhengjie.modules.mnt.util; - -import java.io.*; -import java.util.Enumeration; -import java.util.zip.ZipEntry; -import java.util.zip.ZipException; -import java.util.zip.ZipFile; -import java.util.zip.ZipInputStream; - -/** - * @author: ZhangHouYing - * @date: 2019-08-10 13:34 - */ -public class ZipUtils { - /** - * 解压文件 - * - * @param zipFilePath 解压文件路径 - * @param outputFolder 输出解压文件路径 - */ - public static void unZipIt(String zipFilePath, String outputFolder) { - byte[] buffer = new byte[1024]; - - File folder = new File(outputFolder); - if (!folder.exists()) { - folder.mkdir(); - } - try { - //get the zip file content - ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath)); - ZipEntry ze = zis.getNextEntry(); - while (ze != null) { - String fileName = ze.getName(); - File newFile = new File(outputFolder + File.separator + fileName); - System.out.println("file unzip : " + newFile.getAbsoluteFile()); - //大部分网络上的源码,这里没有判断子目录 - if (ze.isDirectory()) { - if (!newFile.mkdirs()) { - System.out.println("was not successful."); - } - } else { - if (!new File(newFile.getParent()).mkdirs()) { - System.out.println("was not successful."); - } - FileOutputStream fos = new FileOutputStream(newFile); - int len; - while ((len = zis.read(buffer)) != -1) { - fos.write(buffer, 0, len); - } - fos.close(); - } - ze = zis.getNextEntry(); - } - zis.closeEntry(); - zis.close(); - System.out.println("Done"); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public static void unzip(File source, String out) throws IOException { - try (ZipInputStream zis = new ZipInputStream(new FileInputStream(source))) { - - ZipEntry entry = zis.getNextEntry(); - - while (entry != null) { - - File file = new File(out, entry.getName()); - - if (entry.isDirectory()) { - if (!file.mkdirs()) { - System.out.println("was not successful."); - } - } else { - File parent = file.getParentFile(); - - if (!parent.exists()) { - if (!parent.mkdirs()) { - System.out.println("was not successful."); - } - } - - try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) { - - byte[] buffer = new byte[Math.toIntExact(entry.getSize())]; - - int location; - - while ((location = zis.read(buffer)) != -1) { - bos.write(buffer, 0, location); - } - } - } - entry = zis.getNextEntry(); - } - } - } - - /** - * 把所有文件都直接解压到指定目录(忽略子文件夹) - * - * @param zipFile - * @param folderPath - * @throws ZipException - * @throws IOException - */ - public static void upZipFile(File zipFile, String folderPath) throws ZipException, IOException { - File desDir = new File(folderPath); - if (!desDir.exists()) { - if (!desDir.mkdirs()) { - System.out.println("was not successful."); - } - } - ZipFile zf = new ZipFile(zipFile); - for (Enumeration entries = zf.entries(); entries.hasMoreElements(); ) { - ZipEntry entry = ((ZipEntry) entries.nextElement()); - InputStream in = zf.getInputStream(entry); - File desFile = new File(folderPath, java.net.URLEncoder.encode(entry.getName(), "UTF-8")); - - if (!desFile.exists()) { - File fileParentDir = desFile.getParentFile(); - if (!fileParentDir.exists()) { - if (!fileParentDir.mkdirs()) { - System.out.println("was not successful."); - } - } - } - - OutputStream out = new FileOutputStream(desFile); - byte[] buffer = new byte[1024 * 1024]; - int realLength = in.read(buffer); - while (realLength != -1) { - out.write(buffer, 0, realLength); - realLength = in.read(buffer); - } - - out.close(); - in.close(); - - } - } -} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/MonitorServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/MonitorServiceImpl.java index fdc43825..7ff2e91b 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/MonitorServiceImpl.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/MonitorServiceImpl.java @@ -17,6 +17,7 @@ package me.zhengjie.modules.system.service.impl; import cn.hutool.core.date.BetweenFormater; import cn.hutool.core.date.DateUtil; +import me.zhengjie.exception.BadRequestException; import me.zhengjie.modules.system.service.MonitorService; import me.zhengjie.utils.ElAdminConstant; import me.zhengjie.utils.FileUtil; @@ -91,7 +92,11 @@ public class MonitorServiceImpl implements MonitorService { diskInfo.put("total", total > 0 ? FileUtil.getSize(total) : "?"); diskInfo.put("available", FileUtil.getSize(available)); diskInfo.put("used", FileUtil.getSize(used)); - diskInfo.put("usageRate", df.format(used/(double)total * 100)); + if(total != 0){ + diskInfo.put("usageRate", df.format(used/(double)total * 100)); + } else { + diskInfo.put("usageRate", 0); + } return diskInfo; } From 8c6571f1c8174e23220e849e3c337630984a96a2 Mon Sep 17 00:00:00 2001 From: Zheng Jie <201507802@qq.com> Date: Fri, 5 Mar 2021 11:58:16 +0800 Subject: [PATCH 3/3] =?UTF-8?q?[=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96](v2.6?= =?UTF-8?q?)=EF=BC=9Aupdate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- eladmin-system/src/main/resources/config/application-dev.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/eladmin-system/src/main/resources/config/application-dev.yml b/eladmin-system/src/main/resources/config/application-dev.yml index d3fbf517..c7188533 100644 --- a/eladmin-system/src/main/resources/config/application-dev.yml +++ b/eladmin-system/src/main/resources/config/application-dev.yml @@ -13,8 +13,6 @@ spring: min-idle: 15 # 最大连接数 max-active: 30 - # 是否自动回收超时连接 - remove-abandoned: true # 超时时间(以秒数为单位) remove-abandoned-timeout: 180 # 获取连接超时时间