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
98969fa1
Commit
98969fa1
authored
4 years ago
by
lal
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'develop' of develop-gitlab.youlingrc.com:8timerv2/8timerapiv200 into lal
parents
12819cca
1993178e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
89 additions
and
0 deletions
+89
-0
src/main/java/cn/timer/api/controller/oss/OSSController.java
+16
-0
src/main/java/cn/timer/api/utils/aliyun/OSSUtil.java
+73
-0
No files found.
src/main/java/cn/timer/api/controller/oss/OSSController.java
View file @
98969fa1
...
...
@@ -8,6 +8,7 @@ import org.apache.ibatis.annotations.Param;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.transaction.annotation.Transactional
;
import
org.springframework.web.bind.annotation.DeleteMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
...
...
@@ -105,6 +106,21 @@ public class OSSController {
}
/**
* 下载
*/
@GetMapping
(
value
=
"/download"
)
@ApiOperation
(
value
=
"通过url直接下载文件"
,
httpMethod
=
"GET"
,
notes
=
"接口发布说明"
)
public
Result
<
String
>
download
(
@CurrentUser
UserBean
userBean
,
@RequestParam
String
url
,
@RequestParam
String
savePath
)
{
try
{
oss
.
download
(
url
,
savePath
);
}
catch
(
Exception
e
)
{
return
ResultUtil
.
error
(
"下载失败"
);
}
return
ResultUtil
.
data
(
"下载成功"
);
}
/**
* 获取私密文件url(设置为10分钟有效)
*/
@PostMapping
(
value
=
"/getpri"
)
...
...
This diff is collapsed.
Click to expand it.
src/main/java/cn/timer/api/utils/aliyun/OSSUtil.java
View file @
98969fa1
...
...
@@ -19,6 +19,9 @@ import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import
com.aliyun.oss.OSS
;
import
com.aliyun.oss.OSSClientBuilder
;
import
com.aliyun.oss.event.ProgressEvent
;
import
com.aliyun.oss.event.ProgressEventType
;
import
com.aliyun.oss.event.ProgressListener
;
import
com.aliyun.oss.model.DeleteObjectsRequest
;
import
com.aliyun.oss.model.DeleteObjectsResult
;
import
com.aliyun.oss.model.GetObjectRequest
;
...
...
@@ -484,4 +487,74 @@ public class OSSUtil {
* // 关闭OSSClient。 ossClient.shutdown();
*/
public
void
download
(
String
url
,
String
savePath
)
{
// Endpoint以杭州为例,其它Region请按实际情况填写。
// String endpoint = "http://oss-cn-shenzhen.aliyuncs.com";
// // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录
// // https://ram.console.aliyun.com 创建RAM账号。
// String accessKeyId = "LTAI4FuaShJWQ1dggsFWG5CC";
// String accessKeySecret = "EJ6qToT4T4u0B5Rb6qrta9WkyGHvGR";
// String bucketName = "8time-v2";
// String objectName = "8timer2.0/117/test/测试.pdf";
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// String url = "http://8time-v2.oss-cn-shenzhen.aliyuncs.com/8timer2.0/310/qyxx/filepath/158872957193802f4a85a-9dd9-483a-9049-86fac5c73bd5.pdf?Expires=4747952308&OSSAccessKeyId=LTAI4FuaShJWQ1dggsFWG5CC&Signature=z%2FNIX0RrxnLuBLAS9olPpxxsCrI%3D";
try
{
// 带进度条的下载。
ossClient
.
getObject
(
new
GetObjectRequest
(
new
URL
(
url
),
null
),
new
File
(
savePath
));
// new File("C:\\Users\\Lenovo\\Desktop\\8小时\\test2.pdf"));
// // 带进度条的下载。
// ossClient.getObject(new GetObjectRequest(bucketName, objectName).<GetObjectRequest>withProgressListener(
// new GetObjectProgressListener()), new File("C:\\Users\\Lenovo\\Desktop\\8小时\\test.pdf"));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
// 关闭OSSClient。
ossClient
.
shutdown
();
}
static
class
GetObjectProgressListener
implements
ProgressListener
{
private
long
bytesRead
=
0
;
private
long
totalBytes
=
-
1
;
private
boolean
succeed
=
false
;
@Override
public
void
progressChanged
(
ProgressEvent
progressEvent
)
{
long
bytes
=
progressEvent
.
getBytes
();
ProgressEventType
eventType
=
progressEvent
.
getEventType
();
switch
(
eventType
)
{
case
TRANSFER_STARTED_EVENT:
System
.
out
.
println
(
"Start to download......"
);
break
;
case
RESPONSE_CONTENT_LENGTH_EVENT:
this
.
totalBytes
=
bytes
;
System
.
out
.
println
(
this
.
totalBytes
+
" bytes in total will be downloaded to a local file"
);
break
;
case
RESPONSE_BYTE_TRANSFER_EVENT:
this
.
bytesRead
+=
bytes
;
if
(
this
.
totalBytes
!=
-
1
)
{
int
percent
=
(
int
)
(
this
.
bytesRead
*
100.0
/
this
.
totalBytes
);
System
.
out
.
println
(
bytes
+
" bytes have been read at this time, download progress: "
+
percent
+
"%("
+
this
.
bytesRead
+
"/"
+
this
.
totalBytes
+
")"
);
}
else
{
System
.
out
.
println
(
bytes
+
" bytes have been read at this time, download ratio: unknown"
+
"("
+
this
.
bytesRead
+
"/...)"
);
}
break
;
case
TRANSFER_COMPLETED_EVENT:
this
.
succeed
=
true
;
System
.
out
.
println
(
"Succeed to download, "
+
this
.
bytesRead
+
" bytes have been transferred in total"
);
break
;
case
TRANSFER_FAILED_EVENT:
System
.
out
.
println
(
"Failed to download, "
+
this
.
bytesRead
+
" bytes have been transferred"
);
break
;
default
:
break
;
}
}
public
boolean
isSucceed
()
{
return
succeed
;
}
}
}
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