Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
A
antai-sport-http-server
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
antai-sport
antai-sport-http-server
Commits
c6aec543
Commit
c6aec543
authored
Apr 21, 2022
by
liming
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
文本编辑器和文件上传
parent
fb1fceca
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
293 additions
and
0 deletions
+293
-0
pom.xml
management-api/pom.xml
+5
-0
UploadController.java
...er/management/api/common/controller/UploadController.java
+28
-0
UploadService.java
...p/server/management/api/common/service/UploadService.java
+37
-0
OssUtils.java
...ntai/sport/http/server/management/api/utils/OssUtils.java
+214
-0
application.yaml
management-api/src/main/resources/application.yaml
+9
-0
No files found.
management-api/pom.xml
View file @
c6aec543
...
...
@@ -27,6 +27,11 @@
<groupId>
org.mapstruct
</groupId>
<artifactId>
mapstruct
</artifactId>
</dependency>
<dependency>
<groupId>
com.aliyun.oss
</groupId>
<artifactId>
aliyun-sdk-oss
</artifactId>
<version>
3.7.0
</version>
</dependency>
</dependencies>
<build>
...
...
management-api/src/main/java/com/antai/sport/http/server/management/api/common/controller/UploadController.java
0 → 100644
View file @
c6aec543
package
com
.
antai
.
sport
.
http
.
server
.
management
.
api
.
common
.
controller
;
import
com.antai.sport.http.server.common.base.Result
;
import
com.antai.sport.http.server.management.api.annotation.LoginUser
;
import
com.antai.sport.http.server.management.api.common.service.UploadService
;
import
com.antai.sport.http.server.repository.sys.entity.SysUser
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.annotation.Resource
;
import
java.util.Map
;
import
static
com
.
antai
.
sport
.
http
.
server
.
common
.
util
.
ResultUtil
.
success
;
@RequestMapping
(
"/upload"
)
@RestController
public
class
UploadController
{
@Resource
private
UploadService
uploadService
;
@ResponseBody
@PostMapping
public
ResponseEntity
<
Result
<
Map
>>
upload
(
@RequestParam
(
"file"
)
MultipartFile
file
,
@LoginUser
SysUser
loginUser
)
{
return
success
(
uploadService
.
upload
(
file
,
loginUser
));
}
}
management-api/src/main/java/com/antai/sport/http/server/management/api/common/service/UploadService.java
0 → 100644
View file @
c6aec543
package
com
.
antai
.
sport
.
http
.
server
.
management
.
api
.
common
.
service
;
import
com.antai.sport.http.server.common.util.UUIDUtil
;
import
com.antai.sport.http.server.management.api.utils.OssUtils
;
import
com.antai.sport.http.server.repository.sys.entity.SysUser
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.annotation.Resource
;
import
java.io.IOException
;
import
java.util.HashMap
;
import
java.util.Map
;
@Service
public
class
UploadService
{
@Resource
private
OssUtils
ossUtils
;
public
Map
<
String
,
String
>
upload
(
MultipartFile
file
,
SysUser
loginUser
)
{
Map
<
String
,
String
>
result
=
new
HashMap
<>();
String
originalFilename
=
file
.
getOriginalFilename
();
//获取文件类型
String
fileExtension
=
originalFilename
.
substring
(
originalFilename
.
lastIndexOf
(
"."
));
//获取文件名
String
fileName
=
originalFilename
.
substring
(
0
,
originalFilename
.
lastIndexOf
(
"."
));
String
url
=
""
;
try
{
url
=
ossUtils
.
uploadFile
(
UUIDUtil
.
createUUID
()
+
"-"
+
fileName
+
fileExtension
,
file
.
getBytes
());
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
result
.
put
(
"url"
,
url
);
return
result
;
}
}
management-api/src/main/java/com/antai/sport/http/server/management/api/utils/OssUtils.java
0 → 100644
View file @
c6aec543
package
com
.
antai
.
sport
.
http
.
server
.
management
.
api
.
utils
;
import
com.aliyun.oss.OSS
;
import
com.aliyun.oss.OSSClientBuilder
;
import
com.aliyun.oss.model.OSSObject
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.stereotype.Service
;
import
javax.annotation.PostConstruct
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.*
;
import
java.net.URLEncoder
;
import
java.util.zip.Adler32
;
import
java.util.zip.CheckedOutputStream
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipOutputStream
;
/**
* @author 潘维吉
* @date 2019/10/16 9:27
* @email 406798106@qq.com
* @description OSS工具类封装 基于阿里云存储服务
*/
@ConditionalOnProperty
(
prefix
=
"aliyun.oss"
,
name
=
{
"enabled"
},
matchIfMissing
=
false
)
@Service
@Slf4j
public
class
OssUtils
{
@Value
(
"${aliyun.oss.access-key-id}"
)
private
String
accessKeyId
;
@Value
(
"${aliyun.oss.access-key-secret}"
)
private
String
accessKeySecret
;
@Value
(
"${aliyun.oss.endpoint:http://oss-cn-beijing.aliyuncs.com}"
)
private
String
endpoint
;
@Value
(
"${aliyun.oss.bucket-name}"
)
private
String
bucketName
;
@Value
(
"${aliyun.oss.cdn-url}"
)
private
String
cdnUrl
;
private
OSS
ossClient
;
@PostConstruct
private
void
initClient
()
{
// 创建OSSClient实例
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// 关闭OSSClient
//ossClient.shutdown();
}
/**
* 上传文件
*
* @param filePath 指定的文件路径 可指定目录(test/1.jpg)
* @param content 内容
*/
public
String
uploadFile
(
String
filePath
,
byte
[]
content
)
{
// 上传内容到指定的存储空间(bucketName)并保存为指定的文件名称(filePath)
ossClient
.
putObject
(
bucketName
,
filePath
,
new
ByteArrayInputStream
(
content
));
return
cdnUrl
+
filePath
;
}
/**
* 下载文件
*/
public
void
downloadFile
(
String
filePath
)
throws
IOException
{
// 调用ossClient.getObject返回一个OSSObject实例,该实例包含文件内容及文件元信息。
OSSObject
ossObject
=
ossClient
.
getObject
(
bucketName
,
filePath
);
// 调用ossObject.getObjectContent获取文件输入流,可读取此输入流获取其内容。
InputStream
content
=
ossObject
.
getObjectContent
();
if
(
content
!=
null
)
{
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
content
));
while
(
true
)
{
String
line
=
reader
.
readLine
();
if
(
line
==
null
)
{
break
;
}
System
.
out
.
println
(
"\n"
+
line
);
}
// 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
content
.
close
();
}
}
/**
* 删除文件
*/
public
void
deleteFile
(
String
filePath
)
{
ossClient
.
deleteObject
(
bucketName
,
filePath
);
}
/**
* 是否存在存储空间 不存在自动创建存储空间
*/
private
void
isExistsBucket
()
{
if
(!
ossClient
.
doesBucketExist
(
bucketName
))
{
// 创建bucket
ossClient
.
createBucket
(
bucketName
);
}
}
/**
* 创建存储空间
*/
private
void
createBucket
(
String
bucketName
)
{
ossClient
.
createBucket
(
bucketName
);
}
/**
* 获取阿里云图片服务缩略压缩图
*
* @param oriUrl 原图地址
* @param width 宽度
* @param height 高度
* @return 缩略图url
*/
public
String
getThumbnailUrl
(
String
oriUrl
,
int
width
,
int
height
)
{
if
(
oriUrl
==
null
||
!
oriUrl
.
toLowerCase
().
startsWith
(
"http"
))
{
return
oriUrl
;
}
// 等比缩放,限定在矩形框外 将图缩略成宽度为 width,高度为 height,按长边优先
StringBuilder
result
=
new
StringBuilder
(
oriUrl
);
result
.
append
(
"?x-oss-process=image/resize,m_lfit,h_"
);
result
.
append
(
height
);
result
.
append
(
",w_"
);
result
.
append
(
width
);
return
result
.
toString
();
}
/**
* 阿里OSS服务器多文件打包ZIP下载,批量下载
*
* @param key 带目录的key 多文件用,号分割
* @param zipFileName zip包文件名
* @param request
* @param response
* @return
*/
public
HttpServletResponse
zipFilesDown
(
String
key
,
String
zipFileName
,
HttpServletRequest
request
,
HttpServletResponse
response
)
{
try
{
String
fileName
=
zipFileName
+
".zip"
;
// 创建临时文件
File
zipFile
=
File
.
createTempFile
(
zipFileName
,
".zip"
);
FileOutputStream
f
=
new
FileOutputStream
(
zipFile
);
CheckedOutputStream
outputStream
=
new
CheckedOutputStream
(
f
,
new
Adler32
());
// 用于将数据压缩成Zip文件格式
ZipOutputStream
zos
=
new
ZipOutputStream
(
outputStream
);
String
[]
keyList
=
key
.
split
(
","
);
for
(
String
ossFile
:
keyList
)
{
ossFile
=
ossFile
.
trim
();
// 获取Object,返回结果为OSSObject对象
OSSObject
ossObject
=
ossClient
.
getObject
(
bucketName
,
ossFile
);
// 读去Object内容 返回
InputStream
inputStream
=
ossObject
.
getObjectContent
();
// 对于每一个要被存放到压缩包的文件,都必须调用ZipOutputStream对象的putNextEntry()方法,确保压缩包里面文件不同名
zos
.
putNextEntry
(
new
ZipEntry
(
ossFile
.
split
(
"/"
)[
1
]));
int
bytesRead
=
0
;
// 向压缩文件中输出数据
while
((
bytesRead
=
inputStream
.
read
())
!=
-
1
)
{
zos
.
write
(
bytesRead
);
}
inputStream
.
close
();
zos
.
closeEntry
();
// 当前文件写完,定位为写入下一条项目
}
zos
.
close
();
String
header
=
request
.
getHeader
(
"User-Agent"
).
toUpperCase
();
if
(
header
.
contains
(
"MSIE"
)
||
header
.
contains
(
"TRIDENT"
)
||
header
.
contains
(
"EDGE"
))
{
fileName
=
URLEncoder
.
encode
(
fileName
,
"utf-8"
);
fileName
=
fileName
.
replace
(
"+"
,
"%20"
);
//IE下载文件名空格变+号问题
}
else
{
fileName
=
new
String
(
fileName
.
getBytes
(),
"ISO8859-1"
);
}
response
.
reset
();
response
.
setContentType
(
"text/plain"
);
response
.
setContentType
(
"application/octet-stream; charset=utf-8"
);
response
.
setHeader
(
"Location"
,
fileName
);
response
.
setHeader
(
"Cache-Control"
,
"max-age=0"
);
response
.
setHeader
(
"Content-Disposition"
,
"attachment; filename="
+
fileName
);
FileInputStream
fis
=
new
FileInputStream
(
zipFile
);
BufferedInputStream
buff
=
new
BufferedInputStream
(
fis
);
BufferedOutputStream
out
=
new
BufferedOutputStream
(
response
.
getOutputStream
());
byte
[]
car
=
new
byte
[
1024
];
int
l
=
0
;
while
(
l
<
zipFile
.
length
())
{
int
j
=
buff
.
read
(
car
,
0
,
1024
);
l
+=
j
;
out
.
write
(
car
,
0
,
j
);
}
// 关闭流
fis
.
close
();
buff
.
close
();
out
.
close
();
ossClient
.
shutdown
();
// 删除临时文件
zipFile
.
delete
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
response
;
}
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
new
OssUtils
().
getThumbnailUrl
(
"https://an-plus-test.oss-cn-beijing.aliyuncs.com/qr_code/748323084095721472.jpg"
,
750
,
750
));
}
}
management-api/src/main/resources/application.yaml
View file @
c6aec543
...
...
@@ -69,6 +69,15 @@ aliyun:
time-out
:
1000
template-code
:
login
:
SMS_136440120
#登录确认验证码
oss
:
region-id
:
cn-beijing
access-key-id
:
LTAI4Fq5tjMLps5529Vnp7Kz
access-key-secret
:
W1UAzbKf5Ufo6lrFVTe6hicKPoSBSG
enabled
:
true
endpoint
:
https://oss-cn-beijing.aliyuncs.com
bucket-name
:
antai-sport
cdn-url
:
http://antai-sport.oss-cn-beijing.aliyuncs.com/
arn
:
acs:ram::1484442943946857:role/oss
mybatis-plus
:
configuration
:
...
...
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