Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
H
haoban-manage3.0
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
haoban3.0
haoban-manage3.0
Commits
382061d2
Commit
382061d2
authored
May 22, 2023
by
jinxin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
好办运维后台上传图片接口
parent
a1d544dc
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
96 additions
and
0 deletions
+96
-0
UploadController.java
...om/gic/haoban/manage/web/controller/UploadController.java
+96
-0
No files found.
haoban-manage3-operation-web/src/main/java/com/gic/haoban/manage/web/controller/UploadController.java
0 → 100644
View file @
382061d2
package
com
.
gic
.
haoban
.
manage
.
web
.
controller
;
import
cn.hutool.crypto.SecureUtil
;
import
com.alibaba.fastjson.JSONObject
;
import
com.gic.commons.util.DateUtil
;
import
com.gic.haoban.common.utils.HaobanResponse
;
import
com.gic.haoban.common.utils.UploadUtils
;
import
com.gic.haoban.manage.web.errCode.HaoBanErrCode
;
import
com.gic.qcloud.BucketNameEnum
;
import
com.gic.thirdparty.cloudfile.CloudFileUtil
;
import
com.gic.thirdparty.cloudfile.enums.CloudFileBusinessOptEnum
;
import
com.gic.thirdparty.cloudfile.enums.CloudFileTypeEnum
;
import
com.gic.thirdparty.cloudfile.pojo.CloudFileInfo
;
import
org.apache.commons.io.FileUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.time.LocalDateTime
;
import
java.time.format.DateTimeFormatter
;
import
java.util.HashSet
;
import
java.util.Set
;
@RestController
public
class
UploadController
extends
WebBaseController
{
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
UploadController
.
class
);
private
static
Set
<
String
>
FILE_TYPE
=
new
HashSet
<>();
static
{
for
(
BucketNameEnum
value
:
BucketNameEnum
.
values
())
{
FILE_TYPE
.
add
(
value
.
getName
());
}
}
@RequestMapping
(
"/upload-file"
)
@ResponseBody
public
HaobanResponse
upload
(
@RequestParam
MultipartFile
file
)
throws
Exception
{
if
(
file
==
null
||
file
.
isEmpty
())
{
return
resultResponse
(
HaoBanErrCode
.
ERR_2
);
}
String
name
=
file
.
getOriginalFilename
();
// 未加密前文件名称
String
mediaName
=
file
.
getOriginalFilename
();
mediaName
=
mediaName
.
substring
(
0
,
mediaName
.
lastIndexOf
(
"."
));
if
(
mediaName
.
length
()
>
20
)
{
mediaName
=
mediaName
.
substring
(
0
,
20
);
}
String
suffix
=
name
.
substring
(
name
.
lastIndexOf
(
"."
)
+
1
);
name
=
SecureUtil
.
md5
(
name
)
+
"."
+
suffix
;
String
location
=
System
.
getProperty
(
"user.dir"
)
+
"/data/tmp/"
;
long
len
=
file
.
getSize
();
File
templateFile
=
new
File
(
location
+
name
);
FileUtils
.
copyInputStreamToFile
(
file
.
getInputStream
(),
templateFile
);
// 2.上传腾讯云
String
dayFilePath
=
LocalDateTime
.
now
().
format
(
DateTimeFormatter
.
ofPattern
(
DateUtil
.
FORMAT_DATETIME_14
));
String
nameUpload
=
mediaName
+
"."
+
suffix
;
String
key
=
"haoban/"
+
dayFilePath
+
"/"
+
nameUpload
;
try
{
//替换新的上传接口
// String url = FileUploadUtil.simpleUploadFileFromLocal(templateFile, key, fileType);
CloudFileTypeEnum
cloudFileTypeEnum
=
CloudFileTypeEnum
.
OTHER
;
if
(
UploadUtils
.
isPicture
(
suffix
))
{
cloudFileTypeEnum
=
CloudFileTypeEnum
.
IMAGE
;
}
else
if
(
UploadUtils
.
isOffice
(
suffix
))
{
cloudFileTypeEnum
=
CloudFileTypeEnum
.
FILE
;
}
else
if
(
UploadUtils
.
isMusic
(
suffix
))
{
cloudFileTypeEnum
=
CloudFileTypeEnum
.
AUDIO
;
}
else
if
(
UploadUtils
.
isVedio
(
suffix
))
{
cloudFileTypeEnum
=
CloudFileTypeEnum
.
VIDEO
;
}
CloudFileInfo
cloudFileInfo
=
CloudFileUtil
.
uploadFile
(
new
FileInputStream
(
templateFile
),
suffix
,
cloudFileTypeEnum
,
"gic_inner"
,
CloudFileBusinessOptEnum
.
HAOBAN_COMMON
);
if
(
templateFile
!=
null
)
{
templateFile
.
delete
();
}
JSONObject
res
=
new
JSONObject
();
res
.
put
(
"name"
,
name
);
res
.
put
(
"mediaName"
,
mediaName
);
res
.
put
(
"ext"
,
suffix
);
res
.
put
(
"size"
,
len
);
res
.
put
(
"key"
,
key
);
res
.
put
(
"url"
,
cloudFileInfo
.
getOrgFileUrl
());
return
resultResponse
(
HaoBanErrCode
.
ERR_1
,
res
);
}
catch
(
Exception
e
)
{
logger
.
info
(
"异常:{}"
,
e
.
getMessage
(),
e
);
return
resultResponse
(
HaoBanErrCode
.
ERR_0
);
}
}
}
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