Skip to content

Commit e3998a5

Browse files
committed
上传、下载文件进度条添加.
1 parent e6e84b9 commit e3998a5

File tree

3 files changed

+182
-6
lines changed

3 files changed

+182
-6
lines changed

api-boot-project/api-boot-plugins/api-boot-plugin-alibaba-oss/src/main/java/org/minbox/framework/api/boot/plugin/oss/ApiBootOssService.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import com.aliyun.oss.OSSClient;
44
import com.aliyun.oss.model.*;
5-
import lombok.AllArgsConstructor;
5+
import lombok.Setter;
6+
import org.minbox.framework.api.boot.plugin.oss.progress.ApiBootObjectStorageProgress;
7+
import org.minbox.framework.api.boot.plugin.oss.progress.OssProgressListener;
68
import org.minbox.framework.api.boot.plugin.storage.ApiBootObjectStorageService;
79
import org.minbox.framework.api.boot.plugin.storage.exception.ApiBootObjectStorageException;
810
import org.minbox.framework.api.boot.plugin.storage.response.ApiBootObjectStorageResponse;
@@ -27,7 +29,6 @@
2729
* Gitee:https://gitee.com/hengboy
2830
* GitHub:https://github.com/hengboy
2931
*/
30-
@AllArgsConstructor
3132
public class ApiBootOssService implements ApiBootObjectStorageService {
3233
/**
3334
* 地域性的endpoint
@@ -49,13 +50,26 @@ public class ApiBootOssService implements ApiBootObjectStorageService {
4950
* 自定义域名
5051
*/
5152
protected String domain;
53+
/**
54+
* ApiBoot Oss Progress
55+
*/
56+
@Setter
57+
private ApiBootObjectStorageProgress apiBootObjectStorageProgress;
58+
59+
public ApiBootOssService(String endpoint, String bucketName, String accessKeyId, String accessKeySecret, String domain) {
60+
this.endpoint = endpoint;
61+
this.bucketName = bucketName;
62+
this.accessKeyId = accessKeyId;
63+
this.accessKeySecret = accessKeySecret;
64+
this.domain = domain;
65+
}
5266

5367
@Override
5468
public ApiBootObjectStorageResponse upload(String objectName, byte[] bytes) throws ApiBootObjectStorageException {
5569
try {
5670
OSSClient ossClient = getOssClient();
5771
// put byte inputStream
58-
ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));
72+
ossClient.putObject(new PutObjectRequest(bucketName, objectName, new ByteArrayInputStream(bytes)).withProgressListener(new OssProgressListener(objectName, apiBootObjectStorageProgress)));
5973
closeOssClient(ossClient);
6074
} catch (Exception e) {
6175
throw new ApiBootObjectStorageException(e.getMessage(), e);
@@ -68,7 +82,7 @@ public ApiBootObjectStorageResponse upload(String objectName, InputStream inputS
6882
try {
6983
OSSClient ossClient = getOssClient();
7084
// put byte inputStream
71-
ossClient.putObject(bucketName, objectName, inputStream);
85+
ossClient.putObject(new PutObjectRequest(bucketName, objectName, inputStream).withProgressListener(new OssProgressListener(objectName, apiBootObjectStorageProgress)));
7286
closeOssClient(ossClient);
7387
} catch (Exception e) {
7488
throw new ApiBootObjectStorageException(e.getMessage(), e);
@@ -81,7 +95,7 @@ public ApiBootObjectStorageResponse upload(String objectName, String localFile)
8195
try {
8296
OSSClient ossClient = getOssClient();
8397
// put byte inputStream
84-
ossClient.putObject(bucketName, objectName, new File(localFile));
98+
ossClient.putObject(new PutObjectRequest(bucketName, objectName, new File(localFile)).withProgressListener(new OssProgressListener(objectName, apiBootObjectStorageProgress)));
8599
closeOssClient(ossClient);
86100
} catch (Exception e) {
87101
throw new ApiBootObjectStorageException(e.getMessage(), e);
@@ -93,7 +107,7 @@ public ApiBootObjectStorageResponse upload(String objectName, String localFile)
93107
public void download(String objectName, String localFile) throws ApiBootObjectStorageException {
94108
try {
95109
OSSClient ossClient = getOssClient();
96-
ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File(localFile));
110+
ossClient.getObject(new GetObjectRequest(bucketName, objectName).withProgressListener(new OssProgressListener(objectName, apiBootObjectStorageProgress)), new File(localFile));
97111
closeOssClient(ossClient);
98112
} catch (Exception e) {
99113
throw new ApiBootObjectStorageException(e.getMessage(), e);
@@ -242,4 +256,6 @@ protected String getObjectUrl(String objectName) {
242256
}
243257
return getDefaultObjectUrl(objectName);
244258
}
259+
260+
245261
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.oss.progress;
19+
20+
import org.minbox.framework.api.boot.plugin.storage.exception.ApiBootObjectStorageException;
21+
22+
/**
23+
* ApiBoot Oss Upload & Download Progress
24+
*
25+
* @author:恒宇少年 - 于起宇
26+
* <p>
27+
* DateTime:2019-04-16 17:05
28+
* Blog:http://blog.yuqiyu.com
29+
* WebSite:http://www.jianshu.com/u/092df3f77bca
30+
* Gitee:https://gitee.com/hengboy
31+
* GitHub:https://github.com/hengboy
32+
*/
33+
public interface ApiBootObjectStorageProgress {
34+
/**
35+
* progress
36+
*
37+
* @param objectName object name
38+
* @param percent upload or download progress percent
39+
* @param totalBytes total bytes
40+
* @param currentWrittenBytes already written bytes
41+
* @throws ApiBootObjectStorageException ApiBoot Oss Exception
42+
*/
43+
void progress(String objectName, double percent, long totalBytes, long currentWrittenBytes) throws ApiBootObjectStorageException;
44+
45+
/**
46+
* upload or download success
47+
*
48+
* @param objectName object name
49+
* @throws ApiBootObjectStorageException ApiBoot Oss Exception
50+
*/
51+
void success(String objectName) throws ApiBootObjectStorageException;
52+
53+
/**
54+
* upload or download failed
55+
*
56+
* @param objectName object name
57+
* @throws ApiBootObjectStorageException ApiBoot Oss Exception
58+
*/
59+
void failed(String objectName) throws ApiBootObjectStorageException;
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.oss.progress;
19+
20+
import com.aliyun.oss.event.ProgressEvent;
21+
import com.aliyun.oss.event.ProgressEventType;
22+
import com.aliyun.oss.event.ProgressListener;
23+
24+
import java.math.BigDecimal;
25+
26+
/**
27+
* ApiBoot Oss Progress Listener
28+
*
29+
* @author:恒宇少年 - 于起宇
30+
* <p>
31+
* DateTime:2019-04-16 16:05
32+
* Blog:http://blog.yuqiyu.com
33+
* WebSite:http://www.jianshu.com/u/092df3f77bca
34+
* Gitee:https://gitee.com/hengboy
35+
* GitHub:https://github.com/hengboy
36+
*/
37+
public class OssProgressListener implements ProgressListener {
38+
/**
39+
* already write bytes
40+
*/
41+
private long bytesWritten = 0;
42+
/**
43+
* file total bytes
44+
*/
45+
private long totalBytes = -1;
46+
/**
47+
* percent scale
48+
*/
49+
private int percentScale = 2;
50+
/**
51+
* oss object name
52+
*/
53+
private String objectName;
54+
/**
55+
* ApiBoot Progress
56+
*/
57+
private ApiBootObjectStorageProgress apiBootObjectStorageProgress;
58+
59+
public OssProgressListener(String objectName, ApiBootObjectStorageProgress apiBootObjectStorageProgress) {
60+
this.objectName = objectName;
61+
this.apiBootObjectStorageProgress = apiBootObjectStorageProgress;
62+
}
63+
64+
@Override
65+
public void progressChanged(ProgressEvent progressEvent) {
66+
if (apiBootObjectStorageProgress != null) {
67+
// current progress bytes
68+
long bytes = progressEvent.getBytes();
69+
// progress event type
70+
ProgressEventType eventType = progressEvent.getEventType();
71+
72+
switch (eventType) {
73+
// sent file total bytes
74+
case REQUEST_CONTENT_LENGTH_EVENT:
75+
this.totalBytes = bytes;
76+
break;
77+
// request byte transfer
78+
case REQUEST_BYTE_TRANSFER_EVENT:
79+
this.bytesWritten += bytes;
80+
if (this.totalBytes != -1) {
81+
// Calculation percent
82+
double percent = (this.bytesWritten * 100.00 / this.totalBytes);
83+
BigDecimal decimal = BigDecimal.valueOf(percent).setScale(percentScale, BigDecimal.ROUND_DOWN);
84+
apiBootObjectStorageProgress.progress(objectName, decimal.doubleValue(), this.totalBytes, this.bytesWritten);
85+
}
86+
break;
87+
// complete
88+
case TRANSFER_COMPLETED_EVENT:
89+
apiBootObjectStorageProgress.success(objectName);
90+
break;
91+
// failed
92+
case TRANSFER_FAILED_EVENT:
93+
apiBootObjectStorageProgress.failed(objectName);
94+
break;
95+
default:
96+
break;
97+
}
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)