Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
8
8timerapiv200
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
8timerv2
8timerapiv200
Commits
e5fd486e
Commit
e5fd486e
authored
4 years ago
by
Your Name
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
更新了上传文件的出现的问题
parent
96ac7c96
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
97 additions
and
5 deletions
+97
-5
src/main/java/cn/timer/api/service/OSSService.java
+57
-0
src/main/java/cn/timer/api/service/impl/OSSServiceImpl.java
+9
-0
src/main/java/cn/timer/api/utils/aliyun/OSSUtil.java
+4
-5
src/main/java/cn/timer/api/utils/random/RandomNum.java
+27
-0
No files found.
src/main/java/cn/timer/api/service/OSSService.java
0 → 100644
View file @
e5fd486e
package
cn
.
timer
.
api
.
service
;
import
cn.timer.api.service.impl.OSSServiceImpl
;
import
cn.timer.api.utils.Result
;
import
cn.timer.api.utils.ResultUtil
;
import
cn.timer.api.utils.aliyun.OSSUtil
;
import
cn.timer.api.utils.random.RandomNum
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.File
;
import
java.io.IOException
;
/**
* 上传文件的实现层
*/
@Service
public
class
OSSService
implements
OSSServiceImpl
{
@Autowired
private
OSSUtil
oss
;
/**
*
* @param orgCode
* @param moudle
* @param file
* @return
*/
@Override
public
Result
<
String
>
upload
(
int
orgCode
,
String
moudle
,
MultipartFile
file
)
{
String
url
=
null
;
String
randomNickname
=
RandomNum
.
getRandomNickname
();
File
filePath
=
filePathUpload
(
orgCode
,
moudle
,
randomNickname
,
file
.
getOriginalFilename
());
int
i
=
0
;
while
(
filePath
.
exists
())
{
filePath
=
filePathUpload
(
orgCode
,
moudle
,
randomNickname
,
file
.
getOriginalFilename
());
i
++;
}
if
(
file
==
null
||
file
.
getSize
()
<=
0
)
{
return
ResultUtil
.
error
(
"上传的文件为空,请重新选择!"
);
}
else
{
try
{
url
=
oss
.
uploadFile
(
filePath
,
file
.
getInputStream
());
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
return
ResultUtil
.
data
(
url
,
"上传成功!"
);
}
}
public
static
File
filePathUpload
(
int
orgCode
,
String
moudle
,
String
randomNickname
,
String
originalFilename
){
String
path
=
"8timer2.0/"
+
orgCode
+
"/"
+
moudle
+
"/"
+
randomNickname
+
"/"
+
originalFilename
;
File
filePath
=
new
File
(
path
);
return
filePath
;
}
}
This diff is collapsed.
Click to expand it.
src/main/java/cn/timer/api/service/impl/OSSServiceImpl.java
0 → 100644
View file @
e5fd486e
package
cn
.
timer
.
api
.
service
.
impl
;
import
cn.timer.api.utils.Result
;
import
org.springframework.web.multipart.MultipartFile
;
public
interface
OSSServiceImpl
{
public
Result
<
String
>
upload
(
int
orgCode
,
String
moudle
,
MultipartFile
file
);
}
This diff is collapsed.
Click to expand it.
src/main/java/cn/timer/api/utils/aliyun/OSSUtil.java
View file @
e5fd486e
...
...
@@ -101,7 +101,7 @@ public class OSSUtil {
* @param ins 输入流
* @return
*/
public
String
uploadFile
(
String
path
,
InputStream
ins
)
{
public
String
uploadFile
(
File
path
,
InputStream
ins
)
{
// <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
// String objectName = "8time-v2/test/123.jpg";
// 创建OSSClient实例。
...
...
@@ -112,7 +112,7 @@ public class OSSUtil {
// 创建PutObjectRequest对象。
// 项目名+用户公司id+模块名+文件夹名+文件名
PutObjectRequest
putObjectRequest
=
new
PutObjectRequest
(
bucketName
,
path
,
ins
);
PutObjectRequest
putObjectRequest
=
new
PutObjectRequest
(
bucketName
,
path
.
getPath
()
,
ins
);
// [file1,file2,file3],["mk1/img","mk1"]
// 如果需要上传时设置存储类型与访问权限,请参考以下示例代码。
// ObjectMetadata metadata = new ObjectMetadata();
...
...
@@ -129,7 +129,7 @@ public class OSSUtil {
Date
now
=
new
Date
();
Date
expiration
=
DateUtil
.
offset
(
now
,
DateField
.
YEAR
,
100
);
// 一百年之后过期
// 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
URL
url
=
ossClient
.
generatePresignedUrl
(
bucketName
,
path
,
expiration
);
URL
url
=
ossClient
.
generatePresignedUrl
(
bucketName
,
path
.
getPath
()
,
expiration
);
Logoutput
(
String
.
valueOf
(
url
));
// 关闭OSSClient。
...
...
@@ -180,7 +180,6 @@ public class OSSUtil {
/**
* 4.从OSS下载文件
*
* @param objectName 完整路径的文件名
* @param path 本地储存文件路径
* @param orgCode 公司id
* @param dir 包名
...
...
@@ -236,7 +235,7 @@ public class OSSUtil {
/**
* 文件输入流批量上传
*
* @param
userBean
获取参数
* @param
request
获取参数
* @param files 批量文件
* @return 返回上传成功或失败的信息
*/
...
...
This diff is collapsed.
Click to expand it.
src/main/java/cn/timer/api/utils/random/RandomNum.java
0 → 100644
View file @
e5fd486e
package
cn
.
timer
.
api
.
utils
.
random
;
import
java.util.Random
;
public
class
RandomNum
{
/**
* java生成随机数字和字母组合10位数
* @return
*/
public
static
String
getRandomNickname
()
{
String
val
=
""
;
Random
random
=
new
Random
();
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
// 输出字母还是数字
String
charOrNum
=
random
.
nextInt
(
2
)
%
2
==
0
?
"char"
:
"num"
;
// 字符串
if
(
"char"
.
equalsIgnoreCase
(
charOrNum
))
{
// 取得大写字母还是小写字母
int
choice
=
random
.
nextInt
(
2
)
%
2
==
0
?
65
:
97
;
val
+=
(
char
)
(
choice
+
random
.
nextInt
(
26
));
}
else
if
(
"num"
.
equalsIgnoreCase
(
charOrNum
))
{
// 数字
val
+=
String
.
valueOf
(
random
.
nextInt
(
10
));
}
}
return
val
;
}
}
This diff is collapsed.
Click to expand it.
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