Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
fgqyxxlr
Overview
Overview
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
yaru
fgqyxxlr
Commits
e16f3a9a
Commit
e16f3a9a
authored
May 17, 2019
by
RuoYi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
支持上传任意格式文件
parent
96d8e621
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
85 additions
and
16 deletions
+85
-16
ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java
+85
-16
No files found.
ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java
View file @
e16f3a9a
...
...
@@ -2,10 +2,12 @@ package com.ruoyi.common.utils.file;
import
java.io.File
;
import
java.io.IOException
;
import
org.apache.commons.io.FilenameUtils
;
import
org.springframework.web.multipart.MultipartFile
;
import
com.ruoyi.common.config.Global
;
import
com.ruoyi.common.exception.file.FileNameLengthLimitExceededException
;
import
com.ruoyi.common.exception.file.FileSizeLimitExceededException
;
import
com.ruoyi.common.exception.file.InvalidExtensionException
;
import
com.ruoyi.common.utils.DateUtils
;
import
com.ruoyi.common.utils.security.Md5Utils
;
...
...
@@ -31,10 +33,29 @@ public class FileUploadUtils
*/
private
static
String
defaultBaseDir
=
Global
.
getProfile
();
/**
* 默认文件类型jpg
*/
public
static
final
String
IMAGE_JPG_EXTENSION
=
".jpg"
;
public
static
final
String
[]
IMAGE_EXTENSION
=
{
"bmp"
,
"gif"
,
"jpg"
,
"jpeg"
,
"png"
};
public
static
final
String
[]
FLASH_EXTENSION
=
{
"swf"
,
"flv"
};
public
static
final
String
[]
MEDIA_EXTENSION
=
{
"swf"
,
"flv"
,
"mp3"
,
"wav"
,
"wma"
,
"wmv"
,
"mid"
,
"avi"
,
"mpg"
,
"asf"
,
"rm"
,
"rmvb"
};
public
static
final
String
[]
DEFAULT_ALLOWED_EXTENSION
=
{
//图片
"bmp"
,
"gif"
,
"jpg"
,
"jpeg"
,
"png"
,
//word excel powerpoint
"doc"
,
"docx"
,
"xls"
,
"xlsx"
,
"ppt"
,
"pptx"
,
"html"
,
"htm"
,
"txt"
,
//压缩文件
"rar"
,
"zip"
,
"gz"
,
"bz2"
,
//pdf
"pdf"
};
private
static
int
counter
=
0
;
...
...
@@ -59,7 +80,7 @@ public class FileUploadUtils
{
try
{
return
upload
(
getDefaultBaseDir
(),
file
,
FileUploadUtils
.
IMAGE_JPG
_EXTENSION
);
return
upload
(
getDefaultBaseDir
(),
file
,
DEFAULT_ALLOWED
_EXTENSION
);
}
catch
(
Exception
e
)
{
...
...
@@ -79,7 +100,7 @@ public class FileUploadUtils
{
try
{
return
upload
(
baseDir
,
file
,
FileUploadUtils
.
IMAGE_JPG
_EXTENSION
);
return
upload
(
baseDir
,
file
,
DEFAULT_ALLOWED
_EXTENSION
);
}
catch
(
Exception
e
)
{
...
...
@@ -97,36 +118,40 @@ public class FileUploadUtils
* @throws FileSizeLimitExceededException 如果超出最大大小
* @throws FileNameLengthLimitExceededException 文件名太长
* @throws IOException 比如读写文件出错时
* @throws InvalidExtensionException 文件校验异常
*/
public
static
final
String
upload
(
String
baseDir
,
MultipartFile
file
,
String
e
xtension
)
throws
FileSizeLimitExceededException
,
IOException
,
FileNameLengthLimitExceededException
public
static
final
String
upload
(
String
baseDir
,
MultipartFile
file
,
String
[]
allowedE
xtension
)
throws
FileSizeLimitExceededException
,
IOException
,
FileNameLengthLimitExceededException
,
InvalidExtensionException
{
int
fileNamelength
=
file
.
getOriginalFilename
().
length
();
if
(
fileNamelength
>
FileUploadUtils
.
DEFAULT_FILE_NAME_LENGTH
)
{
throw
new
FileNameLengthLimitExceededException
(
FileUploadUtils
.
DEFAULT_FILE_NAME_LENGTH
);
}
assertAllowed
(
file
);
assertAllowed
(
file
,
allowedExtension
);
String
fileName
=
extractFilename
(
file
,
extension
);
String
fileName
=
extractFilename
(
file
);
File
desc
=
getAbsoluteFile
(
baseDir
,
baseDir
+
fileName
);
File
desc
=
getAbsoluteFile
(
baseDir
,
fileName
);
file
.
transferTo
(
desc
);
return
fileName
;
}
public
static
final
String
extractFilename
(
MultipartFile
file
,
String
extension
)
/**
* 编码文件名
*/
public
static
final
String
extractFilename
(
MultipartFile
file
)
{
String
filename
=
file
.
getOriginalFilename
();
filename
=
DateUtils
.
datePath
()
+
"/"
+
encodingFilename
(
filename
)
+
extension
;
String
extension
=
FilenameUtils
.
getExtension
(
file
.
getOriginalFilename
());
filename
=
DateUtils
.
datePath
()
+
File
.
separator
+
encodingFilename
(
filename
)
+
"."
+
extension
;
return
filename
;
}
private
static
final
File
getAbsoluteFile
(
String
uploadDir
,
String
filename
)
throws
IOException
{
File
desc
=
new
File
(
File
.
separator
+
filename
);
File
desc
=
new
File
(
uploadDir
+
File
.
separator
+
filename
);
if
(!
desc
.
getParentFile
().
exists
())
{
...
...
@@ -155,13 +180,57 @@ public class FileUploadUtils
* @param file 上传的文件
* @return
* @throws FileSizeLimitExceededException 如果超出最大大小
* @throws InvalidExtensionException
*/
public
static
final
void
assertAllowed
(
MultipartFile
file
)
throws
FileSizeLimitExceededException
public
static
final
void
assertAllowed
(
MultipartFile
file
,
String
[]
allowedExtension
)
throws
FileSizeLimitExceededException
,
InvalidExtensionException
{
long
size
=
file
.
getSize
();
if
(
DEFAULT_MAX_SIZE
!=
-
1
&&
size
>
DEFAULT_MAX_SIZE
)
{
throw
new
FileSizeLimitExceededException
(
DEFAULT_MAX_SIZE
/
1024
/
1024
);
}
String
filename
=
file
.
getOriginalFilename
();
String
extension
=
FilenameUtils
.
getExtension
(
file
.
getOriginalFilename
());
if
(
allowedExtension
!=
null
&&
!
isAllowedExtension
(
extension
,
allowedExtension
))
{
if
(
allowedExtension
==
IMAGE_EXTENSION
)
{
throw
new
InvalidExtensionException
.
InvalidImageExtensionException
(
allowedExtension
,
extension
,
filename
);
}
else
if
(
allowedExtension
==
FLASH_EXTENSION
)
{
throw
new
InvalidExtensionException
.
InvalidFlashExtensionException
(
allowedExtension
,
extension
,
filename
);
}
else
if
(
allowedExtension
==
MEDIA_EXTENSION
)
{
throw
new
InvalidExtensionException
.
InvalidMediaExtensionException
(
allowedExtension
,
extension
,
filename
);
}
else
{
throw
new
InvalidExtensionException
(
allowedExtension
,
extension
,
filename
);
}
}
}
/**
* 判断MIME类型是否是允许的MIME类型
*
* @param extension
* @param allowedExtension
* @return
*/
public
static
final
boolean
isAllowedExtension
(
String
extension
,
String
[]
allowedExtension
)
{
for
(
String
str
:
allowedExtension
)
{
if
(
str
.
equalsIgnoreCase
(
extension
))
{
return
true
;
}
}
return
false
;
}
}
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