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
56410421
Commit
56410421
authored
May 20, 2020
by
Quxl
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
http://key@gitlab.linkfern.com/key/common.git
parents
91c1da32
a2463789
Hide 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 @
56410421
...
...
@@ -14,9 +14,13 @@ import java.io.IOException;
import
java.io.InputStream
;
import
java.io.ObjectInputStream
;
import
java.io.ObjectOutputStream
;
import
java.io.OutputStream
;
import
java.io.OutputStreamWriter
;
import
java.io.UnsupportedEncodingException
;
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.Paths
;
import
java.util.HashMap
;
...
...
@@ -77,7 +81,7 @@ public class FileUtil {
throw
new
FileUtilException
(
e
);
}
}
public
final
static
String
contentType
(
String
fullName
)
{
try
{
return
Files
.
probeContentType
(
Paths
.
get
(
fullName
));
...
...
@@ -85,16 +89,16 @@ public class FileUtil {
throw
new
FileUtilException
(
e
);
}
}
public
final
static
String
getExtName
(
File
file
)
{
String
extName
=
null
;
String
fileName
=
file
.
getName
();
if
(
fileName
.
contains
(
"."
))
{
if
(
fileName
.
contains
(
"."
))
{
extName
=
fileName
.
substring
(
fileName
.
lastIndexOf
(
"."
)
+
1
).
toUpperCase
();
}
return
extName
;
}
public
final
static
String
imageType
(
File
imageFile
)
{
if
(
isImage
(
imageFile
))
{
ImageInputStream
iis
=
null
;
...
...
@@ -126,7 +130,7 @@ public class FileUtil {
InputStream
is
=
null
;
try
{
String
extName
=
getExtName
(
file
);
if
(
StringUtil
.
isNotBlank
(
extName
))
{
if
(
StringUtil
.
isNotBlank
(
extName
))
{
return
extName
;
}
else
{
is
=
new
FileInputStream
(
file
);
...
...
@@ -137,7 +141,7 @@ public class FileUtil {
return
getExtName
(
file
);
}
finally
{
try
{
if
(
is
!=
null
)
{
if
(
is
!=
null
)
{
is
.
close
();
}
}
catch
(
IOException
e
)
{
...
...
@@ -145,7 +149,7 @@ public class FileUtil {
}
}
}
public
final
static
String
fileType
(
byte
[]
b
)
{
String
filetypeHex
=
String
.
valueOf
(
fileHex
(
b
));
Iterator
<
Entry
<
String
,
String
>>
entryiterator
=
FILE_TYPE_MAP
.
entrySet
().
iterator
();
...
...
@@ -193,16 +197,17 @@ public class FileUtil {
}
return
stringBuilder
.
toString
();
}
/**
* 将字节数组转化为文件
*
* @param bytes
* @param filename
* @return
*/
public
static
File
bytesToFile
(
byte
[]
bytes
,
File
file
)
{
File
parent
=
file
.
getParentFile
();
if
(!
parent
.
exists
())
{
if
(!
parent
.
exists
())
{
parent
.
mkdirs
();
}
BufferedOutputStream
stream
=
null
;
...
...
@@ -223,27 +228,29 @@ public class FileUtil {
}
return
file
;
}
public
static
File
bytesToFile
(
byte
[]
bytes
,
String
filename
)
{
return
bytesToFile
(
bytes
,
new
File
(
filename
));
}
/**
* 将字节数组转化为文件
*
* @param bytes
* @param filenames
* @return
*/
public
static
File
[]
bytesToFiles
(
byte
[][]
bytes
,
String
[]
filenames
)
{
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
]);
}
return
files
;
}
/**
* 将文件转化为字节数组
*
* @param file
* @return
*/
...
...
@@ -265,28 +272,29 @@ public class FileUtil {
}
return
bytes
;
}
/**
* 将文件转化为字节数组
*
* @param files
* @return
*/
public
static
byte
[][]
filesToBytes
(
File
[]
files
)
{
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
]);
}
return
datas
;
}
/**
* 复制文件或文件夹
*
* @param src 源文件
* @param des 目标文件
* @throws IOException 异常时抛出
*/
public
static
void
fileCopy
(
File
src
,
File
des
)
{
/**
* 复制文件或文件夹
*
* @param src 源文件
* @param des 目标文件
* @throws IOException 异常时抛出
*/
public
static
void
fileCopy
(
File
src
,
File
des
)
{
if
(!
src
.
exists
())
{
return
;
}
...
...
@@ -318,35 +326,37 @@ public class FileUtil {
fileCopy
(
sf
,
new
File
(
des
.
getAbsolutePath
()
+
File
.
separator
+
sf
.
getName
()));
}
}
}
}
/**
* 移动文件
*
* @param src 原文件
* @param des 目标文件
* @throws IOException
* @throws IOException
*/
public
static
void
fileMove
(
File
src
,
File
des
)
{
if
(
src
!=
null
)
{
if
(
src
!=
null
)
{
File
parent
=
des
.
getParentFile
();
if
(!
parent
.
exists
())
{
if
(!
parent
.
exists
())
{
parent
.
mkdirs
();
}
if
(!
src
.
renameTo
(
des
))
{
fileCopy
(
src
,
des
);
fileDelete
(
src
);
if
(!
src
.
renameTo
(
des
))
{
fileCopy
(
src
,
des
);
fileDelete
(
src
);
}
}
else
{
throw
new
FileUtilException
(
"要移动的源文件不存在:"
+
src
);
}
}
/**
* 删除文件
*
* @param file 目标文件
*/
public
static
void
fileDelete
(
File
...
files
)
{
for
(
File
file
:
files
)
{
for
(
File
file
:
files
)
{
if
(
file
.
exists
())
{
if
(
file
.
isFile
())
{
file
.
delete
();
...
...
@@ -359,7 +369,7 @@ public class FileUtil {
}
}
}
public
static
byte
[]
streamToBytes
(
InputStream
instream
)
{
byte
[]
bytes
=
null
;
ByteArrayOutputStream
baos
=
null
;
...
...
@@ -382,29 +392,29 @@ public class FileUtil {
}
return
bytes
;
}
public
static
File
streamToFile
(
InputStream
instream
,
File
file
)
{
FileUtil
.
createFile
(
file
);
byte
[]
bytes
=
FileUtil
.
streamToBytes
(
instream
);
return
FileUtil
.
bytesToFile
(
bytes
,
file
);
}
public
static
File
streamToFile
(
InputStream
instream
,
String
fileName
)
{
return
FileUtil
.
streamToFile
(
instream
,
new
File
(
fileName
));
}
public
static
void
stringToFile
(
File
file
,
String
...
strings
)
{
FileOutputStream
fos
=
null
;
OutputStreamWriter
osw
=
null
;
Writer
writer
=
null
;
try
{
if
(!
file
.
getParentFile
().
exists
())
{
if
(!
file
.
getParentFile
().
exists
())
{
file
.
getParentFile
().
mkdirs
();
}
fos
=
new
FileOutputStream
(
file
);
osw
=
new
OutputStreamWriter
(
fos
,
"UTF-8"
);
writer
=
new
BufferedWriter
(
osw
);
for
(
String
string
:
strings
)
{
for
(
String
string
:
strings
)
{
writer
.
append
(
string
).
append
(
System
.
getProperty
(
"line.separator"
));
}
}
catch
(
IOException
e
)
{
...
...
@@ -419,11 +429,11 @@ public class FileUtil {
}
}
}
public
static
void
stringToFile
(
String
fileFullName
,
String
...
strings
)
{
FileUtil
.
stringToFile
(
new
File
(
fileFullName
),
strings
);
}
public
static
String
fileToString
(
File
file
,
String
charset
)
{
try
{
return
new
String
(
FileUtil
.
fileToBytes
(
file
),
charset
);
...
...
@@ -431,34 +441,34 @@ public class FileUtil {
throw
new
FileUtilException
(
"Unsupported charset:"
+
charset
,
e
);
}
}
public
static
String
fileToString
(
String
fileFullName
,
String
charset
)
{
return
FileUtil
.
fileToString
(
new
File
(
fileFullName
),
charset
);
}
public
static
String
fileToString
(
File
file
)
{
return
FileUtil
.
fileToString
(
file
,
"UTF-8"
);
}
public
static
String
fileToString
(
String
fileFullName
)
{
return
FileUtil
.
fileToString
(
new
File
(
fileFullName
));
}
public
static
File
fileFromClasspath
(
String
name
)
{
String
fullName
=
FileUtil
.
class
.
getResource
(
name
).
getPath
();
return
new
File
(
fullName
);
}
public
static
File
createFile
(
String
name
)
{
return
FileUtil
.
createFile
(
new
File
(
name
));
}
public
static
File
createFile
(
File
file
)
{
File
folder
=
file
.
getParentFile
();
if
(!(
folder
.
exists
()
&&
folder
.
isDirectory
()))
{
if
(!(
folder
.
exists
()
&&
folder
.
isDirectory
()))
{
folder
.
mkdirs
();
}
if
(!
file
.
exists
()
||
file
.
isDirectory
())
{
if
(!
file
.
exists
()
||
file
.
isDirectory
())
{
try
{
file
.
createNewFile
();
}
catch
(
IOException
e
)
{
...
...
@@ -469,15 +479,15 @@ public class FileUtil {
}
return
file
;
}
public
static
File
createFolder
(
String
name
)
{
File
folder
=
new
File
(
name
);
if
(!
folder
.
exists
()
||
folder
.
isFile
())
{
if
(!
folder
.
exists
()
||
folder
.
isFile
())
{
folder
.
mkdirs
();
}
return
folder
;
}
public
static
void
objectToFile
(
Object
obj
,
File
file
)
{
FileOutputStream
fos
=
null
;
ObjectOutputStream
oos
=
null
;
...
...
@@ -498,22 +508,22 @@ public class FileUtil {
}
}
}
public
static
InputStream
bytesToInputStream
(
byte
[]
bytes
)
{
return
new
ByteArrayInputStream
(
bytes
);
}
/**
* 序列化
*
* @param object
* @return
* @throws IOException
*/
public
static
byte
[]
objToBytes
(
Object
object
)
throws
IOException
{
ObjectOutputStream
oos
=
null
;
ByteArrayOutputStream
baos
=
null
;
try
{
* 序列化
*
* @param object
* @return
* @throws IOException
*/
public
static
byte
[]
objToBytes
(
Object
object
)
throws
IOException
{
ObjectOutputStream
oos
=
null
;
ByteArrayOutputStream
baos
=
null
;
try
{
baos
=
new
ByteArrayOutputStream
();
oos
=
new
ObjectOutputStream
(
baos
);
oos
.
writeObject
(
object
);
...
...
@@ -525,20 +535,20 @@ public class FileUtil {
baos
.
flush
();
baos
.
close
();
}
}
/**
* 反序列化
*
* @param bytes
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
public
static
Object
bytesToObj
(
byte
[]
bytes
)
throws
IOException
,
ClassNotFoundException
{
ByteArrayInputStream
bais
=
null
;
ObjectInputStream
ois
=
null
;
try
{
}
/**
* 反序列化
*
* @param bytes
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
public
static
Object
bytesToObj
(
byte
[]
bytes
)
throws
IOException
,
ClassNotFoundException
{
ByteArrayInputStream
bais
=
null
;
ObjectInputStream
ois
=
null
;
try
{
bais
=
new
ByteArrayInputStream
(
bytes
);
ois
=
new
ObjectInputStream
(
bais
);
return
ois
.
readObject
();
...
...
@@ -546,13 +556,12 @@ public class FileUtil {
ois
.
close
();
bais
.
close
();
}
}
}
public
static
boolean
isNotExists
(
String
jspname
)
{
return
!
new
File
(
jspname
).
exists
();
}
public
static
Object
fileToObject
(
File
file
)
{
FileInputStream
fis
=
null
;
ObjectInputStream
ois
=
null
;
...
...
@@ -575,15 +584,63 @@ 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
)
{
try
{
//
打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
FileWriter
writer
=
new
FileWriter
(
fileName
,
true
);
writer
.
write
(
content
+
"\n"
);
writer
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
try
{
//
打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
FileWriter
writer
=
new
FileWriter
(
fileName
,
true
);
writer
.
write
(
content
+
"\n"
);
writer
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
\ No newline at end of file
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