Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
C
chnmuseum-party
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
liqin
chnmuseum-party
Commits
183e710d
Commit
183e710d
authored
Mar 25, 2021
by
liqin
💬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bug fixed
parent
7b3c1d86
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
82 additions
and
14 deletions
+82
-14
FastDFSUtils.java
...cn/wisenergy/chnmuseum/party/common/dfs/FastDFSUtils.java
+16
-4
FileTypeEnum.java
.../wisenergy/chnmuseum/party/common/enums/FileTypeEnum.java
+1
-1
FileTypeUtil.java
...n/wisenergy/chnmuseum/party/common/util/FileTypeUtil.java
+0
-5
FileUploadController.java
.../chnmuseum/party/web/controller/FileUploadController.java
+65
-4
No files found.
src/main/java/cn/wisenergy/chnmuseum/party/common/dfs/FastDFSUtils.java
View file @
183e710d
...
@@ -25,7 +25,10 @@ import java.io.ByteArrayInputStream;
...
@@ -25,7 +25,10 @@ import java.io.ByteArrayInputStream;
import
java.io.ByteArrayOutputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
java.util.*
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.Map
;
import
java.util.Set
;
@Slf4j
@Slf4j
@Component
@Component
...
@@ -51,10 +54,19 @@ public class FastDFSUtils {
...
@@ -51,10 +54,19 @@ public class FastDFSUtils {
FastDFSUtils
.
imageConfig
=
thumbImageConfig
;
FastDFSUtils
.
imageConfig
=
thumbImageConfig
;
}
}
public
static
String
uploadFile
(
InputStream
inputStream
,
long
size
,
String
fileName
)
{
final
Set
<
MetaData
>
metaDataSet
=
new
HashSet
<>();
metaDataSet
.
add
(
new
MetaData
(
"fileName"
,
"fileName"
));
metaDataSet
.
add
(
new
MetaData
(
"fileSize"
,
size
+
""
));
metaDataSet
.
add
(
new
MetaData
(
"fileExtName"
,
FilenameUtils
.
getExtension
(
fileName
)));
final
StorePath
storePath
=
storageClient
.
uploadFile
(
inputStream
,
size
,
FilenameUtils
.
getExtension
(
fileName
),
metaDataSet
);
return
dfsFileAccessBasePath
+
"/"
+
storePath
.
getFullPath
();
}
/**
/**
* 字节流方式上传
* 字节流方式上传
*/
*/
public
static
Map
<
String
,
Object
>
uploadImage
(
InputStream
inputStream
,
long
fileSize
,
String
fileName
)
{
public
static
Map
<
String
,
Object
>
upload
Ue
Image
(
InputStream
inputStream
,
long
fileSize
,
String
fileName
)
{
boolean
isImage
=
FileTypeUtil
.
isImageByExtension
(
fileName
);
boolean
isImage
=
FileTypeUtil
.
isImageByExtension
(
fileName
);
if
(
isImage
)
{
if
(
isImage
)
{
String
mimeType
=
FileUtil
.
getMimeType
(
fileName
);
String
mimeType
=
FileUtil
.
getMimeType
(
fileName
);
...
@@ -71,12 +83,12 @@ public class FastDFSUtils {
...
@@ -71,12 +83,12 @@ public class FastDFSUtils {
throw
new
InterfaceException
(
"400"
,
"文件不是图片类型"
);
throw
new
InterfaceException
(
"400"
,
"文件不是图片类型"
);
}
}
public
static
String
upload
File
(
InputStream
inputStream
,
long
size
,
String
fileName
)
{
public
static
String
upload
Video
(
InputStream
inputStream
,
long
size
,
String
fileName
)
{
Map
<
String
,
Object
>
map
=
CopyStreamUtils
.
copyInputStream
(
inputStream
);
Map
<
String
,
Object
>
map
=
CopyStreamUtils
.
copyInputStream
(
inputStream
);
String
md5
=
(
String
)
map
.
get
(
"md5"
);
String
md5
=
(
String
)
map
.
get
(
"md5"
);
InputStream
is
=
(
InputStream
)
map
.
get
(
"inputStream"
);
InputStream
is
=
(
InputStream
)
map
.
get
(
"inputStream"
);
final
LinkedHashSet
<
MetaData
>
metaDataSet
=
new
Linked
HashSet
<>();
final
Set
<
MetaData
>
metaDataSet
=
new
HashSet
<>();
metaDataSet
.
add
(
new
MetaData
(
"fileName"
,
"fileName"
));
metaDataSet
.
add
(
new
MetaData
(
"fileName"
,
"fileName"
));
metaDataSet
.
add
(
new
MetaData
(
"fileSize"
,
size
+
""
));
metaDataSet
.
add
(
new
MetaData
(
"fileSize"
,
size
+
""
));
metaDataSet
.
add
(
new
MetaData
(
"fileExtName"
,
FilenameUtils
.
getExtension
(
fileName
)));
metaDataSet
.
add
(
new
MetaData
(
"fileExtName"
,
FilenameUtils
.
getExtension
(
fileName
)));
...
...
src/main/java/cn/wisenergy/chnmuseum/party/common/enums/FileTypeEnum.java
View file @
183e710d
...
@@ -2,7 +2,7 @@ package cn.wisenergy.chnmuseum.party.common.enums;
...
@@ -2,7 +2,7 @@ package cn.wisenergy.chnmuseum.party.common.enums;
public
enum
FileTypeEnum
{
public
enum
FileTypeEnum
{
D
OC
(
"Doc"
,
"文档
"
),
D
ATUM
(
"Datum"
,
"资料
"
),
IMAGE
(
"Image"
,
"图片"
),
IMAGE
(
"Image"
,
"图片"
),
AUDIO
(
"Audio"
,
"音频"
),
AUDIO
(
"Audio"
,
"音频"
),
VIDEO
(
"Video"
,
"视频"
);
VIDEO
(
"Video"
,
"视频"
);
...
...
src/main/java/cn/wisenergy/chnmuseum/party/common/util/FileTypeUtil.java
View file @
183e710d
...
@@ -95,8 +95,6 @@ public class FileTypeUtil {
...
@@ -95,8 +95,6 @@ public class FileTypeUtil {
is
.
read
(
b
);
is
.
read
(
b
);
fileType
=
getFileTypeByStream
(
b
);
fileType
=
getFileTypeByStream
(
b
);
is
.
close
();
is
.
close
();
}
catch
(
FileNotFoundException
e
)
{
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
...
@@ -140,9 +138,6 @@ public class FileTypeUtil {
...
@@ -140,9 +138,6 @@ public class FileTypeUtil {
ImageReader
reader
=
iter
.
next
();
ImageReader
reader
=
iter
.
next
();
iis
.
close
();
iis
.
close
();
return
reader
.
getFormatName
();
return
reader
.
getFormatName
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
return
null
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
return
null
;
return
null
;
...
...
src/main/java/cn/wisenergy/chnmuseum/party/web/controller/FileUploadController.java
View file @
183e710d
...
@@ -45,6 +45,9 @@ public class FileUploadController {
...
@@ -45,6 +45,9 @@ public class FileUploadController {
// 允许上传的格式
// 允许上传的格式
private
static
final
String
[]
VIDEO_TYPE
=
new
String
[]{
"MP4"
,
"FLV"
};
private
static
final
String
[]
VIDEO_TYPE
=
new
String
[]{
"MP4"
,
"FLV"
};
// 允许上传的格式
private
static
final
String
[]
DATUM_TYPE
=
new
String
[]{
"PDF"
,
"DOC"
,
"DOCX"
,
"XLS"
,
"XLSX"
,
"PPT"
,
"PPTX"
,
"JPG"
,
"JPEG"
,
"PNG"
,
"BMP"
,
"WBMP"
,
"MP4"
,
"FLV"
};
// @RequestMapping(value = "/upload", method = RequestMethod.POST)
// @RequestMapping(value = "/upload", method = RequestMethod.POST)
// public ResponseEntity<PicUploadResult> upload(@RequestParam(value = "bc_cover", required = true) MultipartFile uploadFile, HttpServletResponse response) throws Exception {
// public ResponseEntity<PicUploadResult> upload(@RequestParam(value = "bc_cover", required = true) MultipartFile uploadFile, HttpServletResponse response) throws Exception {
// // 校验文件扩展名
// // 校验文件扩展名
...
@@ -95,9 +98,67 @@ public class FileUploadController {
...
@@ -95,9 +98,67 @@ public class FileUploadController {
// return ResponseEntity.status(HttpStatus.CREATED).body(fileUploadResult);
// return ResponseEntity.status(HttpStatus.CREATED).body(fileUploadResult);
// }
// }
@PostMapping
(
value
=
"/upload"
,
headers
=
"content-type=multipart/form-data"
,
consumes
=
MediaType
.
MULTIPART_FORM_DATA_VALUE
)
@ApiImplicitParams
({
@ApiImplicitParam
(
name
=
"files"
,
value
=
"资料"
,
paramType
=
"form"
,
dataType
=
"__file"
,
collectionFormat
=
"array"
,
allowMultiple
=
true
)
})
@PostMapping
(
value
=
"/file/upload"
,
headers
=
"content-type=multipart/form-data"
,
consumes
=
MediaType
.
MULTIPART_FORM_DATA_VALUE
)
@ApiOperation
(
value
=
"资料上传"
,
notes
=
"资料上传"
)
public
ResponseEntity
<
BatchUploadResVO
>
uploadFile
(
@RequestPart
(
value
=
"files"
,
required
=
false
)
MultipartFile
[]
files
)
throws
IOException
{
if
(
files
.
length
==
0
)
{
throw
new
InterfaceException
(
RESPONSE_CODE_ENUM
.
REQUEST_PARAMS_ERROR
.
getCode
(),
"没有文件可供上传"
);
}
int
successCount
=
0
;
int
failureCount
=
0
;
List
<
BatchUploadResVO
.
HandleResult
>
handleList
=
new
ArrayList
<>();
List
<
String
>
videoUrlList
=
new
ArrayList
<>();
for
(
MultipartFile
file
:
files
)
{
// 当前维度表下线结果
BatchUploadResVO
.
HandleResult
handleResult
=
new
BatchUploadResVO
.
HandleResult
();
// 原始文件名
String
originalFilename
=
file
.
getOriginalFilename
();
if
(
StringUtils
.
isBlank
(
originalFilename
))
{
handleResult
.
setFileName
(
""
);
handleResult
.
setFileType
(
FileTypeEnum
.
DATUM
.
getName
());
handleResult
.
setFileUrl
(
""
);
handleResult
.
setHandleResult
(
HANDLE_STATUS_ENUM
.
FAILURE
.
getName
());
handleResult
.
setDescription
(
"文件名为空"
);
failureCount
++;
continue
;
}
handleResult
.
setFileName
(
originalFilename
);
handleResult
.
setFileType
(
FileTypeEnum
.
DATUM
.
getName
());
handleResult
.
setHandleResult
(
HANDLE_STATUS_ENUM
.
SUCCESS
.
getName
());
boolean
anyMatch
=
Arrays
.
stream
(
DATUM_TYPE
).
anyMatch
(
s
->
Objects
.
equals
(
s
,
FilenameUtils
.
getExtension
(
originalFilename
).
toUpperCase
()));
if
(
anyMatch
)
{
String
url
=
FastDFSUtils
.
uploadFile
(
file
.
getInputStream
(),
file
.
getSize
(),
originalFilename
);
handleResult
.
setFileUrl
(
url
);
handleResult
.
setDescription
(
"操作成功"
);
videoUrlList
.
add
(
url
);
successCount
++;
}
else
{
handleResult
.
setFileUrl
(
""
);
handleResult
.
setDescription
(
"文件格式不支持"
);
failureCount
++;
}
// 设置处理的业务表信息
handleList
.
add
(
handleResult
);
}
BatchUploadResVO
batchUploadResVO
=
new
BatchUploadResVO
();
batchUploadResVO
.
setFailureCount
(
failureCount
);
batchUploadResVO
.
setSuccessCount
(
successCount
);
batchUploadResVO
.
setTotal
(
files
.
length
);
batchUploadResVO
.
setHandleList
(
handleList
);
batchUploadResVO
.
setUrlList
(
videoUrlList
);
return
ResponseEntity
.
status
(
HttpStatus
.
CREATED
).
body
(
batchUploadResVO
);
}
@PostMapping
(
value
=
"/image/upload"
,
headers
=
"content-type=multipart/form-data"
,
consumes
=
MediaType
.
MULTIPART_FORM_DATA_VALUE
)
@RequiresPermissions
(
"image:upload"
)
@ApiOperation
(
value
=
"单图片上传"
,
notes
=
"单图片上传"
)
@ApiOperation
(
value
=
"单图片上传"
,
notes
=
"单图片上传"
)
public
ResponseEntity
<
ImageUploadResult
>
upload
Fil
e
(
@RequestParam
(
value
=
"cover"
)
MultipartFile
uploadFile
)
throws
Exception
{
public
ResponseEntity
<
ImageUploadResult
>
upload
Imag
e
(
@RequestParam
(
value
=
"cover"
)
MultipartFile
uploadFile
)
throws
Exception
{
if
(
uploadFile
==
null
||
uploadFile
.
getSize
()
==
0
)
{
if
(
uploadFile
==
null
||
uploadFile
.
getSize
()
==
0
)
{
throw
new
InterfaceException
(
RESPONSE_CODE_ENUM
.
REQUEST_PARAMS_ERROR
.
getCode
(),
"没有文件可供上传"
);
throw
new
InterfaceException
(
RESPONSE_CODE_ENUM
.
REQUEST_PARAMS_ERROR
.
getCode
(),
"没有文件可供上传"
);
}
}
...
@@ -126,7 +187,7 @@ public class FileUploadController {
...
@@ -126,7 +187,7 @@ public class FileUploadController {
@PostMapping
(
value
=
"/video/upload"
,
headers
=
"content-type=multipart/form-data"
,
consumes
=
MediaType
.
MULTIPART_FORM_DATA_VALUE
)
@PostMapping
(
value
=
"/video/upload"
,
headers
=
"content-type=multipart/form-data"
,
consumes
=
MediaType
.
MULTIPART_FORM_DATA_VALUE
)
@RequiresPermissions
(
"video:upload"
)
@RequiresPermissions
(
"video:upload"
)
@ApiOperation
(
value
=
"多视频上传"
,
notes
=
"多视频上传"
)
@ApiOperation
(
value
=
"多视频上传"
,
notes
=
"多视频上传"
)
public
ResponseEntity
<
BatchUploadResVO
>
upload
File
(
@RequestPart
(
value
=
"files"
,
required
=
false
)
MultipartFile
[]
files
)
throws
IOException
{
public
ResponseEntity
<
BatchUploadResVO
>
upload
Video
(
@RequestPart
(
value
=
"files"
,
required
=
false
)
MultipartFile
[]
files
)
throws
IOException
{
if
(
files
.
length
==
0
)
{
if
(
files
.
length
==
0
)
{
throw
new
InterfaceException
(
RESPONSE_CODE_ENUM
.
REQUEST_PARAMS_ERROR
.
getCode
(),
"没有文件可供上传"
);
throw
new
InterfaceException
(
RESPONSE_CODE_ENUM
.
REQUEST_PARAMS_ERROR
.
getCode
(),
"没有文件可供上传"
);
}
}
...
@@ -160,7 +221,7 @@ public class FileUploadController {
...
@@ -160,7 +221,7 @@ public class FileUploadController {
handleResult
.
setHandleResult
(
HANDLE_STATUS_ENUM
.
SUCCESS
.
getName
());
handleResult
.
setHandleResult
(
HANDLE_STATUS_ENUM
.
SUCCESS
.
getName
());
boolean
anyMatch
=
Arrays
.
stream
(
VIDEO_TYPE
).
anyMatch
(
s
->
Objects
.
equals
(
s
,
FilenameUtils
.
getExtension
(
originalFilename
).
toUpperCase
()));
boolean
anyMatch
=
Arrays
.
stream
(
VIDEO_TYPE
).
anyMatch
(
s
->
Objects
.
equals
(
s
,
FilenameUtils
.
getExtension
(
originalFilename
).
toUpperCase
()));
if
(
anyMatch
)
{
if
(
anyMatch
)
{
String
url
=
FastDFSUtils
.
upload
File
(
file
.
getInputStream
(),
file
.
getSize
(),
originalFilename
);
String
url
=
FastDFSUtils
.
upload
Video
(
file
.
getInputStream
(),
file
.
getSize
(),
originalFilename
);
handleResult
.
setFileUrl
(
url
);
handleResult
.
setFileUrl
(
url
);
handleResult
.
setDescription
(
"操作成功"
);
handleResult
.
setDescription
(
"操作成功"
);
videoUrlList
.
add
(
url
);
videoUrlList
.
add
(
url
);
...
...
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