UploadPartRequestFactory.java
2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.cnlive.cnUploadDemo.utils;
import com.cnlive.libs.upload.services.CNUploadPartRequest;
import java.io.File;
import java.io.Serializable;
/**
* UploadPartRequest工厂,用于分块上传操作
*/
public class UploadPartRequestFactory implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 指定的Object键
*/
private String key;
/**
* 初始化时得到的uploadId
*/
private String uploadId;
/**
* 需要上传的file
*/
private File file;
/**
* 此分块是第几块
*/
public int partNumber = 1;
/**
* 此分块在文件中内容的起始位置
*/
private long offset = 0L;
/**
* 剩余大小
*/
private long remainingBytes;
/**
* 此分块的大小,单位为Byte
*/
public long partSize;
/**
* 已经上传大小
*/
private long uploadedSize;
public UploadPartRequestFactory(String objectKey, String uploadId, File file, long partSize) {
this.uploadId = uploadId;
this.key = objectKey;
this.file = file;
this.remainingBytes = file.length();
this.partSize = partSize;
this.partNumber = 1;
this.uploadedSize = 0;
}
public synchronized boolean hasMoreRequests() {
return this.remainingBytes > 0L;
}
// 计算下一个UploadPartRequest配置参数,并返回对象
public synchronized CNUploadPartRequest getNextUploadPartRequest() {
boolean bool = this.remainingBytes - this.partSize <= 0L;
CNUploadPartRequest localUploadPartRequest = null;
uploadedSize = file.length() - this.remainingBytes;
localUploadPartRequest = new CNUploadPartRequest(key, uploadId, file, offset, partNumber++, partSize);
this.offset += partSize;
this.remainingBytes -= partSize;
localUploadPartRequest.setLastPart(bool);
return localUploadPartRequest;
}
public synchronized long getUploadedSize() {
return this.uploadedSize;
}
public synchronized File getFile() {
return this.file;
}
public synchronized String getUploadId() {
return this.uploadId;
}
public synchronized String getObjectKey() {
return this.key;
}
}