Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
common
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
曲欣亮
common
Commits
33051cde
Commit
33051cde
authored
May 20, 2020
by
张永
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
加一个
parent
a80a68f1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
150 additions
and
93 deletions
+150
-93
FileUtil.java
src/main/java/com/egolm/common/FileUtil.java
+150
-93
No files found.
src/main/java/com/egolm/common/FileUtil.java
View file @
33051cde
...
@@ -14,9 +14,13 @@ import java.io.IOException;
...
@@ -14,9 +14,13 @@ import java.io.IOException;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
java.io.ObjectInputStream
;
import
java.io.ObjectInputStream
;
import
java.io.ObjectOutputStream
;
import
java.io.ObjectOutputStream
;
import
java.io.OutputStream
;
import
java.io.OutputStreamWriter
;
import
java.io.OutputStreamWriter
;
import
java.io.UnsupportedEncodingException
;
import
java.io.UnsupportedEncodingException
;
import
java.io.Writer
;
import
java.io.Writer
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
java.net.URLConnection
;
import
java.nio.file.Files
;
import
java.nio.file.Files
;
import
java.nio.file.Paths
;
import
java.nio.file.Paths
;
import
java.util.HashMap
;
import
java.util.HashMap
;
...
@@ -89,7 +93,7 @@ public class FileUtil {
...
@@ -89,7 +93,7 @@ public class FileUtil {
public
final
static
String
getExtName
(
File
file
)
{
public
final
static
String
getExtName
(
File
file
)
{
String
extName
=
null
;
String
extName
=
null
;
String
fileName
=
file
.
getName
();
String
fileName
=
file
.
getName
();
if
(
fileName
.
contains
(
"."
))
{
if
(
fileName
.
contains
(
"."
))
{
extName
=
fileName
.
substring
(
fileName
.
lastIndexOf
(
"."
)
+
1
).
toUpperCase
();
extName
=
fileName
.
substring
(
fileName
.
lastIndexOf
(
"."
)
+
1
).
toUpperCase
();
}
}
return
extName
;
return
extName
;
...
@@ -126,7 +130,7 @@ public class FileUtil {
...
@@ -126,7 +130,7 @@ public class FileUtil {
InputStream
is
=
null
;
InputStream
is
=
null
;
try
{
try
{
String
extName
=
getExtName
(
file
);
String
extName
=
getExtName
(
file
);
if
(
StringUtil
.
isNotBlank
(
extName
))
{
if
(
StringUtil
.
isNotBlank
(
extName
))
{
return
extName
;
return
extName
;
}
else
{
}
else
{
is
=
new
FileInputStream
(
file
);
is
=
new
FileInputStream
(
file
);
...
@@ -137,7 +141,7 @@ public class FileUtil {
...
@@ -137,7 +141,7 @@ public class FileUtil {
return
getExtName
(
file
);
return
getExtName
(
file
);
}
finally
{
}
finally
{
try
{
try
{
if
(
is
!=
null
)
{
if
(
is
!=
null
)
{
is
.
close
();
is
.
close
();
}
}
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
...
@@ -196,13 +200,14 @@ public class FileUtil {
...
@@ -196,13 +200,14 @@ public class FileUtil {
/**
/**
* 将字节数组转化为文件
* 将字节数组转化为文件
*
* @param bytes
* @param bytes
* @param filename
* @param filename
* @return
* @return
*/
*/
public
static
File
bytesToFile
(
byte
[]
bytes
,
File
file
)
{
public
static
File
bytesToFile
(
byte
[]
bytes
,
File
file
)
{
File
parent
=
file
.
getParentFile
();
File
parent
=
file
.
getParentFile
();
if
(!
parent
.
exists
())
{
if
(!
parent
.
exists
())
{
parent
.
mkdirs
();
parent
.
mkdirs
();
}
}
BufferedOutputStream
stream
=
null
;
BufferedOutputStream
stream
=
null
;
...
@@ -230,13 +235,14 @@ public class FileUtil {
...
@@ -230,13 +235,14 @@ public class FileUtil {
/**
/**
* 将字节数组转化为文件
* 将字节数组转化为文件
*
* @param bytes
* @param bytes
* @param filenames
* @param filenames
* @return
* @return
*/
*/
public
static
File
[]
bytesToFiles
(
byte
[][]
bytes
,
String
[]
filenames
)
{
public
static
File
[]
bytesToFiles
(
byte
[][]
bytes
,
String
[]
filenames
)
{
File
[]
files
=
new
File
[
filenames
.
length
];
File
[]
files
=
new
File
[
filenames
.
length
];
for
(
int
i
=
0
;
i
<
filenames
.
length
;
i
++)
{
for
(
int
i
=
0
;
i
<
filenames
.
length
;
i
++)
{
files
[
i
]
=
bytesToFile
(
bytes
[
i
],
filenames
[
i
]);
files
[
i
]
=
bytesToFile
(
bytes
[
i
],
filenames
[
i
]);
}
}
return
files
;
return
files
;
...
@@ -244,6 +250,7 @@ public class FileUtil {
...
@@ -244,6 +250,7 @@ public class FileUtil {
/**
/**
* 将文件转化为字节数组
* 将文件转化为字节数组
*
* @param file
* @param file
* @return
* @return
*/
*/
...
@@ -268,12 +275,13 @@ public class FileUtil {
...
@@ -268,12 +275,13 @@ public class FileUtil {
/**
/**
* 将文件转化为字节数组
* 将文件转化为字节数组
*
* @param files
* @param files
* @return
* @return
*/
*/
public
static
byte
[][]
filesToBytes
(
File
[]
files
)
{
public
static
byte
[][]
filesToBytes
(
File
[]
files
)
{
byte
[][]
datas
=
new
byte
[
files
.
length
][];
byte
[][]
datas
=
new
byte
[
files
.
length
][];
for
(
int
i
=
0
;
i
<
files
.
length
;
i
++)
{
for
(
int
i
=
0
;
i
<
files
.
length
;
i
++)
{
datas
[
i
]
=
fileToBytes
(
files
[
i
]);
datas
[
i
]
=
fileToBytes
(
files
[
i
]);
}
}
return
datas
;
return
datas
;
...
@@ -322,17 +330,18 @@ public class FileUtil {
...
@@ -322,17 +330,18 @@ public class FileUtil {
/**
/**
* 移动文件
* 移动文件
*
* @param src 原文件
* @param src 原文件
* @param des 目标文件
* @param des 目标文件
* @throws IOException
* @throws IOException
*/
*/
public
static
void
fileMove
(
File
src
,
File
des
)
{
public
static
void
fileMove
(
File
src
,
File
des
)
{
if
(
src
!=
null
)
{
if
(
src
!=
null
)
{
File
parent
=
des
.
getParentFile
();
File
parent
=
des
.
getParentFile
();
if
(!
parent
.
exists
())
{
if
(!
parent
.
exists
())
{
parent
.
mkdirs
();
parent
.
mkdirs
();
}
}
if
(!
src
.
renameTo
(
des
))
{
if
(!
src
.
renameTo
(
des
))
{
fileCopy
(
src
,
des
);
fileCopy
(
src
,
des
);
fileDelete
(
src
);
fileDelete
(
src
);
}
}
...
@@ -343,10 +352,11 @@ public class FileUtil {
...
@@ -343,10 +352,11 @@ public class FileUtil {
/**
/**
* 删除文件
* 删除文件
*
* @param file 目标文件
* @param file 目标文件
*/
*/
public
static
void
fileDelete
(
File
...
files
)
{
public
static
void
fileDelete
(
File
...
files
)
{
for
(
File
file
:
files
)
{
for
(
File
file
:
files
)
{
if
(
file
.
exists
())
{
if
(
file
.
exists
())
{
if
(
file
.
isFile
())
{
if
(
file
.
isFile
())
{
file
.
delete
();
file
.
delete
();
...
@@ -398,13 +408,13 @@ public class FileUtil {
...
@@ -398,13 +408,13 @@ public class FileUtil {
OutputStreamWriter
osw
=
null
;
OutputStreamWriter
osw
=
null
;
Writer
writer
=
null
;
Writer
writer
=
null
;
try
{
try
{
if
(!
file
.
getParentFile
().
exists
())
{
if
(!
file
.
getParentFile
().
exists
())
{
file
.
getParentFile
().
mkdirs
();
file
.
getParentFile
().
mkdirs
();
}
}
fos
=
new
FileOutputStream
(
file
);
fos
=
new
FileOutputStream
(
file
);
osw
=
new
OutputStreamWriter
(
fos
,
"UTF-8"
);
osw
=
new
OutputStreamWriter
(
fos
,
"UTF-8"
);
writer
=
new
BufferedWriter
(
osw
);
writer
=
new
BufferedWriter
(
osw
);
for
(
String
string
:
strings
)
{
for
(
String
string
:
strings
)
{
writer
.
append
(
string
).
append
(
System
.
getProperty
(
"line.separator"
));
writer
.
append
(
string
).
append
(
System
.
getProperty
(
"line.separator"
));
}
}
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
...
@@ -455,10 +465,10 @@ public class FileUtil {
...
@@ -455,10 +465,10 @@ public class FileUtil {
public
static
File
createFile
(
File
file
)
{
public
static
File
createFile
(
File
file
)
{
File
folder
=
file
.
getParentFile
();
File
folder
=
file
.
getParentFile
();
if
(!(
folder
.
exists
()
&&
folder
.
isDirectory
()))
{
if
(!(
folder
.
exists
()
&&
folder
.
isDirectory
()))
{
folder
.
mkdirs
();
folder
.
mkdirs
();
}
}
if
(!
file
.
exists
()
||
file
.
isDirectory
())
{
if
(!
file
.
exists
()
||
file
.
isDirectory
())
{
try
{
try
{
file
.
createNewFile
();
file
.
createNewFile
();
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
...
@@ -472,7 +482,7 @@ public class FileUtil {
...
@@ -472,7 +482,7 @@ public class FileUtil {
public
static
File
createFolder
(
String
name
)
{
public
static
File
createFolder
(
String
name
)
{
File
folder
=
new
File
(
name
);
File
folder
=
new
File
(
name
);
if
(!
folder
.
exists
()
||
folder
.
isFile
())
{
if
(!
folder
.
exists
()
||
folder
.
isFile
())
{
folder
.
mkdirs
();
folder
.
mkdirs
();
}
}
return
folder
;
return
folder
;
...
@@ -552,7 +562,6 @@ public class FileUtil {
...
@@ -552,7 +562,6 @@ public class FileUtil {
return
!
new
File
(
jspname
).
exists
();
return
!
new
File
(
jspname
).
exists
();
}
}
public
static
Object
fileToObject
(
File
file
)
{
public
static
Object
fileToObject
(
File
file
)
{
FileInputStream
fis
=
null
;
FileInputStream
fis
=
null
;
ObjectInputStream
ois
=
null
;
ObjectInputStream
ois
=
null
;
...
@@ -576,11 +585,59 @@ public class FileUtil {
...
@@ -576,11 +585,59 @@ public class FileUtil {
}
}
}
}
public
static
void
urlToFile
(
String
url
,
String
savePath
,
String
fileName
)
{
InputStream
is
=
null
;
OutputStream
os
=
null
;
try
{
// 构造URL
URL
weburl
=
new
URL
(
url
);
// 打开连接
URLConnection
con
=
weburl
.
openConnection
();
// 设置请求超时为5s
con
.
setConnectTimeout
(
5
*
1000
);
// 输入流
is
=
con
.
getInputStream
();
// 1K的数据缓冲
byte
[]
bs
=
new
byte
[
1024
];
// 读取到的数据长度
int
len
;
// 输出的文件流
File
sf
=
new
File
(
savePath
);
if
(!
sf
.
exists
())
{
sf
.
mkdirs
();
}
os
=
new
FileOutputStream
(
savePath
+
"/"
+
fileName
);
// 开始读取
while
((
len
=
is
.
read
(
bs
))
!=
-
1
)
{
os
.
write
(
bs
,
0
,
len
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
os
!=
null
)
{
try
{
os
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
if
(
is
!=
null
)
{
try
{
is
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
public
static
void
appendMethod
(
String
fileName
,
String
content
)
{
public
static
void
appendMethod
(
String
fileName
,
String
content
)
{
try
{
try
{
//
打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
//
打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
FileWriter
writer
=
new
FileWriter
(
fileName
,
true
);
FileWriter
writer
=
new
FileWriter
(
fileName
,
true
);
writer
.
write
(
content
+
"\n"
);
writer
.
write
(
content
+
"\n"
);
writer
.
close
();
writer
.
close
();
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
...
...
Write
Preview
Markdown
is supported
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