Skip to content

Commit 5362045

Browse files
committed
Merge pull request #2 from chzyer/patch-3
Update README.md
2 parents 33db1b9 + f8df993 commit 5362045

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

docs/README.md

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Android SDK只包含了最终用户使用场景中的必要功能。相比服务
6565

6666
该方法的详细说明如下:
6767

68-
```
68+
```java
6969
public void put(String key,
7070
InputStreamAt isa,
7171
com.qiniu.io.PutExtra extra,
@@ -91,7 +91,7 @@ public void put(String key,
9191

9292
以下是一个关于`PutExtra`使用的示例:
9393

94-
```
94+
```java
9595
extra.mimeType = "application/json"; // 强制设置MIME类型
9696

9797
extra.params = new HashMap<String, String>();
@@ -114,7 +114,7 @@ extra.params.put("x:a", "bb"); // 设置一个自定义变量
114114

115115
开发者可以基于分片上传机制实现断点续上传功能。
116116

117-
```
117+
```java
118118
class ResumableIO {
119119
public static void put(String key,
120120
InputStreamAt isa,
@@ -125,7 +125,51 @@ class ResumableIO {
125125

126126
具体用法和`IO.put`的类似。
127127

128-
> TODO: 这个没写完。这一节应该要告诉开发者,要断点续传的话应该持久化哪些东西,然后恢复上传时需要将之前持久化的东西设置到哪里去。
128+
#### 续上传
129+
续上传的进度信息都储存在com.qiniu.resumableio.PutExtra. 所以当上传失败的时候,可以将PutExtra持久化下来,等到下一次上传的时候,再使用这个PutExtra,具体代码实现如下
130+
131+
失败状况
132+
```java
133+
final int PERSIST_PACE = 5;
134+
final PutExtra extra = new PutExtra();
135+
final String key = "key";
136+
final String filepath = "xx/xx/xx";
137+
// 准备上传
138+
db.execute("INSERT INTO `table_resumable_table` (`key`, `filepath`) VALUES ('" + key + "', '" + filepath + "')");
139+
ResumableIO.put(key, InputStreamAt.fromFile(new File(filepath)), extra, new JSONObjectRet() {
140+
int process;
141+
int lastPersistProcess = 0;
142+
private void persist() {
143+
// 持久化
144+
db.execute("UPDATE `table_resumable_table` SET extra='" + extra.toJSON() + "', process=" + process + " WHERE `key`='" + key + "' and `filepath`='" + filepath + "'");
145+
}
146+
public void onSuccess(JSONObject obj) {
147+
// 上传成功,删除记录
148+
db.execute("DELETE FROM `table_resumable_table` WHERE `key`='" + key + "' and `filepath`='" + filepath + "'");
149+
}
150+
public void onProcess(int current, int total) {
151+
process = current*100/total;
152+
// 每5%持久化一次
153+
if (process - lastPersistProcess > PERSIST_PACE) {
154+
persist();
155+
lastPersistProcess = process;
156+
}
157+
}
158+
public void onFailure(Exception ex) {
159+
// 忽略处理exception,
160+
persist();
161+
}
162+
})
163+
```
164+
165+
续传恢复
166+
```java
167+
JSONObject ret = db.GetOne("SELECT * FROM `table_resumable_table` LIMIT 0, 1");
168+
PutExtra extra = new PutExtra(ret.optString("extraJson", ""));
169+
String key = ret.optString("key", "");
170+
String filepath = ret.optString("filepath", "");
171+
ResumableIO.put(key, InputStreamAt.fromFile(new File(filepath)), extra, new JSONObjectRet() {...});
172+
```
129173

130174
<a name="upload-concurrency"></a>
131175
### 上传中的并发性

0 commit comments

Comments
 (0)