[代码优化](v2.6):代码优化

pull/608/head^2
Zheng Jie 2021-03-05 11:26:08 +08:00
parent 01d1aa9721
commit cd85594e48
5 changed files with 94 additions and 194 deletions

View File

@ -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) {
// 静默关闭
}
}
}
}

View File

@ -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));
}
}

View File

@ -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<String> 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<String> 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();
}
/**

View File

@ -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();
}
}
}

View File

@ -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;
}