Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in
Toggle navigation
T
tanpu-community
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
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
探普后端
tanpu-community
Commits
754fa8fe
Commit
754fa8fe
authored
Jun 17, 2021
by
张辰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
上传文件接口
parent
ee5083b7
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
74 additions
and
2 deletions
+74
-2
FileController.java
...n/java/com/tanpu/community/controller/FileController.java
+28
-0
ThemeConvert.java
.../com/tanpu/community/controller/convert/ThemeConvert.java
+2
-0
FileManager.java
...rc/main/java/com/tanpu/community/manager/FileManager.java
+36
-0
OSSFileService.java
...main/java/com/tanpu/community/service/OSSFileService.java
+8
-2
No files found.
community-service/src/main/java/com/tanpu/community/controller/FileController.java
0 → 100644
View file @
754fa8fe
package
com
.
tanpu
.
community
.
controller
;
import
com.tanpu.common.api.CommonResp
;
import
com.tanpu.common.constant.ErrorCodeConstant
;
import
com.tanpu.common.exception.BizException
;
import
com.tanpu.community.dao.entity.community.FileRecordEntity
;
import
com.tanpu.community.manager.FileManager
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.util.Date
;
@RestController
@Slf4j
@RequestMapping
(
value
=
"/api/file"
)
public
class
FileController
{
@Autowired
private
FileManager
fileManager
;
@PostMapping
(
"/uploadFile"
)
@ResponseBody
public
CommonResp
<
FileRecordEntity
>
uploadToRemote
(
@RequestParam
(
value
=
"file"
)
MultipartFile
file
)
{
return
CommonResp
.
success
(
fileManager
.
uploadFile
(
file
));
}
}
community-service/src/main/java/com/tanpu/community/controller/convert/ThemeConvert.java
View file @
754fa8fe
...
...
@@ -2,6 +2,7 @@ package com.tanpu.community.controller.convert;
import
com.tanpu.community.api.beans.ThemeDTO
;
import
com.tanpu.community.dao.entity.community.ThemeEntity
;
import
org.springframework.beans.BeanUtils
;
import
java.util.List
;
import
java.util.stream.Collectors
;
...
...
@@ -9,6 +10,7 @@ import java.util.stream.Collectors;
public
class
ThemeConvert
{
public
static
ThemeDTO
convertToDTO
(
ThemeEntity
themeEntity
){
ThemeDTO
themeDTO
=
new
ThemeDTO
();
BeanUtils
.
copyProperties
(
themeEntity
,
themeDTO
);
themeDTO
.
setId
(
themeEntity
.
getId
());
themeDTO
.
setContent
(
themeEntity
.
getContent
());
themeDTO
.
setThemeType
(
themeEntity
.
getThemeType
());
...
...
community-service/src/main/java/com/tanpu/community/manager/FileManager.java
0 → 100644
View file @
754fa8fe
package
com
.
tanpu
.
community
.
manager
;
import
com.tanpu.common.constant.ErrorCodeConstant
;
import
com.tanpu.common.exception.BizException
;
import
com.tanpu.community.dao.entity.community.FileRecordEntity
;
import
com.tanpu.community.service.OSSFileService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.IOException
;
@Slf4j
@Service
public
class
FileManager
{
@Autowired
private
OSSFileService
ossFileService
;
public
FileRecordEntity
uploadFile
(
MultipartFile
file
)
{
if
(
file
==
null
)
{
throw
new
BizException
(
ErrorCodeConstant
.
FILE_UPLOAD_FAIL
);
}
byte
[]
data
=
null
;
try
{
data
=
file
.
getBytes
();
}
catch
(
IOException
e
)
{
throw
new
BizException
(
ErrorCodeConstant
.
FILE_UPLOAD_FAIL
);
}
String
originalName
=
file
.
getOriginalFilename
();
return
ossFileService
.
uploadFile
(
data
,
originalName
);
}
}
community-service/src/main/java/com/tanpu/community/service/OSSFileService.java
View file @
754fa8fe
...
...
@@ -40,6 +40,12 @@ public class OSSFileService {
ossHelper
=
AliyunOSSHelper
.
build
(
endpoint
,
accessId
,
accessSK
);
}
@Transactional
public
FileRecordEntity
uploadFile
(
byte
[]
data
,
String
fileName
)
{
String
fileSuffix
=
fileName
.
substring
(
fileName
.
lastIndexOf
(
'.'
)
+
1
);
return
uploadFile
(
data
,
fileName
,
fileSuffix
,
null
,
OssRelType
.
None
);
}
@Transactional
public
FileRecordEntity
uploadFile
(
byte
[]
data
,
String
fileName
,
String
relId
,
OssRelType
relType
)
{
String
fileSuffix
=
fileName
.
substring
(
fileName
.
lastIndexOf
(
'.'
)
+
1
);
...
...
@@ -50,8 +56,8 @@ public class OSSFileService {
public
FileRecordEntity
uploadFile
(
byte
[]
data
,
String
fileName
,
String
fileSuffix
,
String
relId
,
OssRelType
relType
)
{
// todo uniqueLong
String
key
=
""
;
String
id
=
""
;
String
key
=
String
.
valueOf
(
System
.
currentTimeMillis
())
;
String
id
=
String
.
valueOf
(
System
.
currentTimeMillis
())
;
ossHelper
.
writeFile
(
bucketName
,
key
,
data
,
fileSuffix
);
...
...
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