Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Eladmin
Commits
cd85594e
Commit
cd85594e
authored
Mar 05, 2021
by
Zheng Jie
Browse files
[代码优化](v2.6):代码优化
parent
01d1aa97
Changes
5
Show whitespace changes
Inline
Side-by-side
eladmin-common/src/main/java/me/zhengjie/utils/CloseUtil.java
0 → 100644
View file @
cd85594e
/*
* 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
)
{
// 静默关闭
}
}
}
}
eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java
View file @
cd85594e
...
...
@@ -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
);
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
);
}
os
.
close
();
ins
.
close
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
CloseUtil
.
close
(
os
);
CloseUtil
.
close
(
ins
);
}
return
file
;
}
...
...
@@ -257,8 +263,11 @@ 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
);
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
(
FileNotFound
Exception
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
));
}
}
eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/SqlUtils.java
View file @
cd85594e
...
...
@@ -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,8 +150,10 @@ public class SqlUtils {
* @param connection /
* @param sqlList /
*/
public
static
void
batchExecute
(
Connection
connection
,
List
<
String
>
sqlList
)
throws
SQLException
{
Statement
st
=
connection
.
createStatement
();
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
);
...
...
@@ -171,6 +161,11 @@ public class SqlUtils {
st
.
addBatch
(
sql
);
}
st
.
executeBatch
();
}
catch
(
SQLException
throwables
)
{
throwables
.
printStackTrace
();
}
finally
{
CloseUtil
.
close
(
st
);
}
}
/**
...
...
eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/ZipUtils.java
deleted
100644 → 0
View file @
01d1aa97
/*
* 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
();
}
}
}
eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/MonitorServiceImpl.java
View file @
cd85594e
...
...
@@ -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
));
if
(
total
!=
0
){
diskInfo
.
put
(
"usageRate"
,
df
.
format
(
used
/(
double
)
total
*
100
));
}
else
{
diskInfo
.
put
(
"usageRate"
,
0
);
}
return
diskInfo
;
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment