Skip to content

Commit cd85594

Browse files
committed
[代码优化](v2.6):代码优化
1 parent 01d1aa9 commit cd85594

File tree

5 files changed

+94
-194
lines changed

5 files changed

+94
-194
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2019-2020 Zheng Jie
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package me.zhengjie.utils;
17+
18+
import java.io.Closeable;
19+
20+
/**
21+
* @author Zheng Jie
22+
* @website https://el-admin.vip
23+
* @description 用于关闭各种连接,缺啥补啥
24+
* @date 2021-03-05
25+
**/
26+
public class CloseUtil {
27+
28+
public static void close(Closeable closeable) {
29+
if (null != closeable) {
30+
try {
31+
closeable.close();
32+
} catch (Exception e) {
33+
// 静默关闭
34+
}
35+
}
36+
}
37+
38+
public static void close(AutoCloseable closeable) {
39+
if (null != closeable) {
40+
try {
41+
closeable.close();
42+
} catch (Exception e) {
43+
// 静默关闭
44+
}
45+
}
46+
}
47+
}

eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,26 @@ public static String getSize(long size) {
153153
/**
154154
* inputStream 转 File
155155
*/
156-
static File inputStreamToFile(InputStream ins, String name) throws Exception {
156+
static File inputStreamToFile(InputStream ins, String name){
157157
File file = new File(SYS_TEM_DIR + name);
158158
if (file.exists()) {
159159
return file;
160160
}
161-
OutputStream os = new FileOutputStream(file);
162-
int bytesRead;
163-
int len = 8192;
164-
byte[] buffer = new byte[len];
165-
while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
166-
os.write(buffer, 0, bytesRead);
161+
OutputStream os = null;
162+
try {
163+
os = new FileOutputStream(file);
164+
int bytesRead;
165+
int len = 8192;
166+
byte[] buffer = new byte[len];
167+
while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
168+
os.write(buffer, 0, bytesRead);
169+
}
170+
} catch (Exception e) {
171+
e.printStackTrace();
172+
} finally {
173+
CloseUtil.close(os);
174+
CloseUtil.close(ins);
167175
}
168-
os.close();
169-
ins.close();
170176
return file;
171177
}
172178

@@ -257,7 +263,10 @@ public static void checkSize(long maxSize, long size) {
257263
public static boolean check(File file1, File file2) {
258264
String img1Md5 = getMd5(file1);
259265
String img2Md5 = getMd5(file2);
260-
return img1Md5.equals(img2Md5);
266+
if(img1Md5 != null){
267+
return img1Md5.equals(img2Md5);
268+
}
269+
return false;
261270
}
262271

263272
/**
@@ -270,16 +279,19 @@ public static boolean check(String file1Md5, String file2Md5) {
270279
private static byte[] getByte(File file) {
271280
// 得到文件长度
272281
byte[] b = new byte[(int) file.length()];
282+
InputStream in = null;
273283
try {
274-
InputStream in = new FileInputStream(file);
284+
in = new FileInputStream(file);
275285
try {
276286
System.out.println(in.read(b));
277287
} catch (IOException e) {
278288
log.error(e.getMessage(), e);
279289
}
280-
} catch (FileNotFoundException e) {
290+
} catch (Exception e) {
281291
log.error(e.getMessage(), e);
282292
return null;
293+
} finally {
294+
CloseUtil.close(in);
283295
}
284296
return b;
285297
}
@@ -341,5 +353,4 @@ public static void downloadFile(HttpServletRequest request, HttpServletResponse
341353
public static String getMd5(File file) {
342354
return getMd5(getByte(file));
343355
}
344-
345356
}

eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/SqlUtils.java

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import com.alibaba.druid.util.StringUtils;
2020
import com.google.common.collect.Lists;
2121
import lombok.extern.slf4j.Slf4j;
22-
22+
import me.zhengjie.utils.CloseUtil;
2323
import javax.sql.DataSource;
2424
import java.io.BufferedReader;
2525
import java.io.File;
@@ -35,9 +35,6 @@
3535
@Slf4j
3636
public class SqlUtils {
3737

38-
public static final String COLON = ":";
39-
40-
4138
/**
4239
* 获取数据源
4340
*
@@ -102,6 +99,8 @@ private static Connection getConnection(String jdbcUrl, String userName, String
10299
} catch (Exception e) {
103100
log.error("create connection error, jdbcUrl: {}", jdbcUrl);
104101
throw new RuntimeException("create connection error, jdbcUrl: " + jdbcUrl);
102+
} finally {
103+
CloseUtil.close(connection);
105104
}
106105
return connection;
107106
}
@@ -117,17 +116,6 @@ private static void releaseConnection(Connection connection) {
117116
}
118117
}
119118

120-
121-
public static void closeResult(ResultSet rs) {
122-
if (rs != null) {
123-
try {
124-
rs.close();
125-
} catch (Exception e) {
126-
log.error(e.getMessage(),e);
127-
}
128-
}
129-
}
130-
131119
public static boolean testConnection(String jdbcUrl, String userName, String password) {
132120
Connection connection = null;
133121
try {
@@ -162,15 +150,22 @@ public static String executeFile(String jdbcUrl, String userName, String passwor
162150
* @param connection /
163151
* @param sqlList /
164152
*/
165-
public static void batchExecute(Connection connection, List<String> sqlList) throws SQLException {
166-
Statement st = connection.createStatement();
167-
for (String sql : sqlList) {
168-
if (sql.endsWith(";")) {
169-
sql = sql.substring(0, sql.length() - 1);
153+
public static void batchExecute(Connection connection, List<String> sqlList) {
154+
Statement st = null;
155+
try {
156+
st = connection.createStatement();
157+
for (String sql : sqlList) {
158+
if (sql.endsWith(";")) {
159+
sql = sql.substring(0, sql.length() - 1);
160+
}
161+
st.addBatch(sql);
170162
}
171-
st.addBatch(sql);
163+
st.executeBatch();
164+
} catch (SQLException throwables) {
165+
throwables.printStackTrace();
166+
} finally {
167+
CloseUtil.close(st);
172168
}
173-
st.executeBatch();
174169
}
175170

176171
/**

eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/ZipUtils.java

Lines changed: 0 additions & 158 deletions
This file was deleted.

eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/MonitorServiceImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import cn.hutool.core.date.BetweenFormater;
1919
import cn.hutool.core.date.DateUtil;
20+
import me.zhengjie.exception.BadRequestException;
2021
import me.zhengjie.modules.system.service.MonitorService;
2122
import me.zhengjie.utils.ElAdminConstant;
2223
import me.zhengjie.utils.FileUtil;
@@ -91,7 +92,11 @@ private Map<String,Object> getDiskInfo(OperatingSystem os) {
9192
diskInfo.put("total", total > 0 ? FileUtil.getSize(total) : "?");
9293
diskInfo.put("available", FileUtil.getSize(available));
9394
diskInfo.put("used", FileUtil.getSize(used));
94-
diskInfo.put("usageRate", df.format(used/(double)total * 100));
95+
if(total != 0){
96+
diskInfo.put("usageRate", df.format(used/(double)total * 100));
97+
} else {
98+
diskInfo.put("usageRate", 0);
99+
}
95100
return diskInfo;
96101
}
97102

0 commit comments

Comments
 (0)