Skip to content

Commit c213b48

Browse files
committed
注释;修改方法名
1 parent cc96e16 commit c213b48

File tree

5 files changed

+98
-41
lines changed

5 files changed

+98
-41
lines changed

library/src/androidTest/java/com/qiniu/android/SyncFormUploadTest.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void testHello() throws Throwable {
3838
params.put("x:foo", "fooval");
3939
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
4040
byte[] b = "hello".getBytes();
41-
info = uploadManager.put(b, expectKey, TestConfig.token, opt);
41+
info = uploadManager.syncPut(b, expectKey, TestConfig.token, opt);
4242
resp = info.response;
4343

4444
Assert.assertTrue(info.toString(), info.isOK());
@@ -57,7 +57,7 @@ public void test0Data() throws Throwable {
5757
params.put("x:foo", "fooval");
5858
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
5959

60-
info = uploadManager.put("".getBytes(), expectKey, TestConfig.token, opt);
60+
info = uploadManager.syncPut("".getBytes(), expectKey, TestConfig.token, opt);
6161
resp = info.response;
6262

6363
// key = resp.optString("key");
@@ -74,7 +74,7 @@ public void testNoKey() throws Throwable {
7474
Map<String, String> params = new HashMap<String, String>();
7575
params.put("x:foo", "fooval");
7676
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
77-
info = uploadManager.put("hello".getBytes(), expectKey, TestConfig.token, opt);
77+
info = uploadManager.syncPut("hello".getBytes(), expectKey, TestConfig.token, opt);
7878

7979
resp = info.response;
8080
key = resp.optString("key");
@@ -88,7 +88,7 @@ public void testNoKey() throws Throwable {
8888
@SmallTest
8989
public void testInvalidToken() throws Throwable {
9090
final String expectKey = "你好";
91-
info = uploadManager.put("hello".getBytes(), expectKey, "invalid", null);
91+
info = uploadManager.syncPut("hello".getBytes(), expectKey, "invalid", null);
9292

9393
resp = info.response;
9494
// key = resp.optString("key");
@@ -102,7 +102,7 @@ public void testInvalidToken() throws Throwable {
102102
public void testNoData() throws Throwable {
103103
final String expectKey = "你好";
104104

105-
info = uploadManager.put((byte[]) null, expectKey, "invalid", null);
105+
info = uploadManager.syncPut((byte[]) null, expectKey, "invalid", null);
106106

107107
resp = info.response;
108108
Assert.assertEquals(info.toString(), ResponseInfo.InvalidArgument,
@@ -113,7 +113,7 @@ public void testNoData() throws Throwable {
113113
@SmallTest
114114
public void testNoToken() throws Throwable {
115115
final String expectKey = "你好";
116-
info = uploadManager.put(new byte[1], expectKey, null, null);
116+
info = uploadManager.syncPut(new byte[1], expectKey, null, null);
117117

118118
resp = info.response;
119119
Assert.assertEquals(info.toString(), ResponseInfo.InvalidArgument, info.statusCode);
@@ -123,7 +123,7 @@ public void testNoToken() throws Throwable {
123123
@SmallTest
124124
public void testEmptyToken() throws Throwable {
125125
final String expectKey = "你好";
126-
info = uploadManager.put(new byte[1], expectKey, "", null);
126+
info = uploadManager.syncPut(new byte[1], expectKey, "", null);
127127

128128
resp = info.response;
129129
Assert.assertEquals(info.toString(), ResponseInfo.InvalidArgument,
@@ -138,7 +138,7 @@ public void testFile() throws Throwable {
138138
Map<String, String> params = new HashMap<String, String>();
139139
params.put("x:foo", "fooval");
140140
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
141-
info = uploadManager.put(f, expectKey, TestConfig.token, opt);
141+
info = uploadManager.syncPut(f, expectKey, TestConfig.token, opt);
142142

143143
resp = info.response;
144144
key = resp.optString("key");
@@ -160,7 +160,7 @@ public void test0File() throws Throwable {
160160
Map<String, String> params = new HashMap<String, String>();
161161
params.put("x:foo", "fooval");
162162
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
163-
info = uploadManager.put(f, expectKey, TestConfig.token, opt);
163+
info = uploadManager.syncPut(f, expectKey, TestConfig.token, opt);
164164

165165
resp = info.response;
166166
Assert.assertEquals(f.toString(), 0, f.length());
@@ -173,10 +173,10 @@ public void test0File() throws Throwable {
173173

174174
@SmallTest
175175
public void testNoComplete() {
176-
info = uploadManager.put(new byte[0], null, null, null);
176+
info = uploadManager.syncPut(new byte[0], null, null, null);
177177
Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode);
178178

179-
info = uploadManager.put("", null, null, null);
179+
info = uploadManager.syncPut("", null, null, null);
180180
Assert.assertEquals(info.toString(), ResponseInfo.ZeroSizeFile, info.statusCode);
181181
}
182182

@@ -194,7 +194,7 @@ public void testIpBack() throws Throwable {
194194
params.put("x:foo", "fooval");
195195
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
196196

197-
info = uploadManager2.put("hello".getBytes(), expectKey, TestConfig.token, opt);
197+
info = uploadManager2.syncPut("hello".getBytes(), expectKey, TestConfig.token, opt);
198198

199199
Assert.assertTrue(info.toString(), info.isOK());
200200
Assert.assertNotNull(info.reqId);
@@ -217,7 +217,7 @@ public void testPortBackup() throws Throwable {
217217
params.put("x:foo", "fooval");
218218
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
219219

220-
info = uploadManager2.put("hello".getBytes(), expectKey, TestConfig.token, opt);
220+
info = uploadManager2.syncPut("hello".getBytes(), expectKey, TestConfig.token, opt);
221221

222222
Assert.assertTrue(info.toString(), info.isOK());
223223
Assert.assertNotNull(info.reqId);
@@ -240,7 +240,7 @@ public void testDnsHijacking() throws Throwable {
240240
params.put("x:foo", "fooval");
241241
final UploadOptions opt = new UploadOptions(params, null, true, null, null);
242242

243-
info = uploadManager2.put("hello".getBytes(), expectKey, TestConfig.token, opt);
243+
info = uploadManager2.syncPut("hello".getBytes(), expectKey, TestConfig.token, opt);
244244

245245
resp = info.response;
246246
Assert.assertTrue(info.toString(), info.isOK());
@@ -261,7 +261,7 @@ public void testHttps() throws Throwable {
261261
.zone(z)
262262
.build();
263263
UploadManager uploadManager2 = new UploadManager(c);
264-
info = uploadManager2.put("hello".getBytes(), expectKey, TestConfig.token, opt);
264+
info = uploadManager2.syncPut("hello".getBytes(), expectKey, TestConfig.token, opt);
265265

266266
resp = info.response;
267267
key = resp.optString("key");

library/src/main/java/com/qiniu/android/http/Client.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,9 +413,6 @@ public void accept(String key, Object value) {
413413

414414
HttpUrl u = req.url();
415415
return new ResponseInfo(null, statusCode, "", "", "", u.host(), u.encodedPath(), "", u.port(), 0, 0, e.getMessage());
416-
// TODO
417-
// return ResponseInfo.create(null, statusCode, "", "", "", u.host(),
418-
// u.encodedPath(), "", u.port(), tag.duration, 0, e.getMessage(), upToken);
419416
}
420417
}
421418

library/src/main/java/com/qiniu/android/http/ResponseInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public final class ResponseInfo {
8686
public final long sent;
8787

8888
/**
89-
* hide, 内部使用
89+
* 响应体,json 格式
9090
*/
9191
public final JSONObject response;
9292

library/src/main/java/com/qiniu/android/storage/FormUploader.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,45 @@ public void complete(ResponseInfo info, JSONObject response) {
150150
client.asyncMultipartPost(config.zone.upHost(token.token).address.toString(), args, progress, completion, options.cancellationSignal);
151151
}
152152

153-
public static ResponseInfo upload(Client client, Configuration config, byte[] data, File file, String key, UpToken token, UploadOptions options) {
153+
/**
154+
* 上传数据,并以指定的key保存文件
155+
*
156+
* @param client HTTP连接管理器
157+
* @param data 上传的数据
158+
* @param key 上传的数据保存的文件名
159+
* @param token 上传凭证
160+
* @param options 上传时的可选参数
161+
*
162+
* @return 响应信息 ResponseInfo#response 响应体,序列化后 json 格式
163+
*/
164+
public static ResponseInfo syncUpload(Client client, Configuration config, byte[] data, String key, UpToken token, UploadOptions options) {
165+
try {
166+
return syncUpload0(client, config, data, null, key, token, options);
167+
} catch (Exception e) {
168+
return new ResponseInfo(null, ResponseInfo.UnknownError, "", "", "", "", "", "", 0, 0, 0, e.getMessage()); // TODO
169+
}
170+
}
171+
172+
/**
173+
* 上传文件,并以指定的key保存文件
174+
*
175+
* @param client HTTP连接管理器
176+
* @param file 上传的文件
177+
* @param key 上传的数据保存的文件名
178+
* @param token 上传凭证
179+
* @param options 上传时的可选参数
180+
*
181+
* @return 响应信息 ResponseInfo#response 响应体,序列化后 json 格式
182+
*/
183+
public static ResponseInfo syncUpload(Client client, Configuration config, File file, String key, UpToken token, UploadOptions options) {
154184
try {
155-
return upload0(client, config, data, file, key, token, options);
185+
return syncUpload0(client, config, null, file, key, token, options);
156186
} catch (Exception e) {
157187
return new ResponseInfo(null, ResponseInfo.UnknownError, "", "", "", "", "", "", 0, 0, 0, e.getMessage()); // TODO
158-
// return ResponseInfo.create(null, ResponseInfo.UnknownError, "", "", "", "",
159-
// "", "", 0, 0, 0, e.getMessage(), token);
160188
}
161189
}
162190

163-
private static ResponseInfo upload0(Client client, Configuration config, byte[] data, File file,
191+
private static ResponseInfo syncUpload0(Client client, Configuration config, byte[] data, File file,
164192
String key, UpToken token, UploadOptions optionsIn) {
165193
StringMap params = new StringMap();
166194
final PostArgs args = new PostArgs();

library/src/main/java/com/qiniu/android/storage/UploadManager.java

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
/**
1313
* 七牛文件上传管理器
14-
* 一般默认可以使用这个类的方法来上传数据和文件。这个类自动检测文件的大小,
15-
* 只要超过了{@link Configuration#putThreshold}
14+
* 一般默认可以使用这个类的方法来上传数据和文件。会自动检测文件的大小,
15+
* 只要超过了{@link Configuration#putThreshold} 异步方法会使用分片方片上传。
16+
* 同步上传方法使用表单方式上传,建议只对小文件使用同步方式
1617
*/
1718
public final class UploadManager {
1819
private final Configuration config;
@@ -196,16 +197,56 @@ public void onFailure(int reason) {
196197

197198
}
198199

199-
public ResponseInfo put(byte[] data, String key, String token, UploadOptions options) {
200-
return put(data, null, key, token, options);
200+
/**
201+
* 同步上传文件。使用 form 表单方式上传,建议只在数据较小情况下使用此方式,如 file.size() < 1024 * 1024。
202+
*
203+
* @param data 上传的数据
204+
* @param key 上传数据保存的文件名
205+
* @param token 上传凭证
206+
* @param options 上传数据的可选参数
207+
*
208+
* @return 响应信息 ResponseInfo#response 响应体,序列化后 json 格式
209+
*/
210+
public ResponseInfo syncPut(byte[] data, String key, String token, UploadOptions options) {
211+
final UpToken decodedToken = UpToken.parse(token);
212+
ResponseInfo info = areInvalidArg(key, data, null, token, decodedToken);
213+
if (info != null) {
214+
return info;
215+
}
216+
return FormUploader.syncUpload(client, config, data, key, decodedToken, options);
201217
}
202218

203-
public ResponseInfo put(File file, String key, String token, UploadOptions options) {
204-
return put(null, file, key, token, options);
219+
/**
220+
* 同步上传文件。使用 form 表单方式上传,建议只在文件较小情况下使用此方式,如 file.size() < 1024 * 1024。
221+
*
222+
* @param file 上传的文件对象
223+
* @param key 上传数据保存的文件名
224+
* @param token 上传凭证
225+
* @param options 上传数据的可选参数
226+
*
227+
* @return 响应信息 ResponseInfo#response 响应体,序列化后 json 格式
228+
*/
229+
public ResponseInfo syncPut(File file, String key, String token, UploadOptions options) {
230+
final UpToken decodedToken = UpToken.parse(token);
231+
ResponseInfo info = areInvalidArg(key, null, file, token, decodedToken);
232+
if (info != null) {
233+
return info;
234+
}
235+
return FormUploader.syncUpload(client, config, file, key, decodedToken, options);
205236
}
206237

207-
public ResponseInfo put(String file, String key, String token, UploadOptions options) {
208-
return put(new File(file), key, token, options);
238+
/**
239+
* 同步上传文件。使用 form 表单方式上传,建议只在文件较小情况下使用此方式,如 file.size() < 1024 * 1024。
240+
*
241+
* @param file 上传的文件绝对路径
242+
* @param key 上传数据保存的文件名
243+
* @param token 上传凭证
244+
* @param options 上传数据的可选参数
245+
*
246+
* @return 响应信息 ResponseInfo#response 响应体,序列化后 json 格式
247+
*/
248+
public ResponseInfo syncPut(String file, String key, String token, UploadOptions options) {
249+
return syncPut(new File(file), key, token, options);
209250
}
210251

211252
private static ResponseInfo areInvalidArg(final String key, byte[] data, File f, String token,
@@ -229,13 +270,4 @@ private static ResponseInfo areInvalidArg(final String key, byte[] data, File f,
229270
return info;
230271
}
231272

232-
private ResponseInfo put(final byte[] data, final File file, final String key, String token, final UploadOptions options) {
233-
final UpToken decodedToken = UpToken.parse(token);
234-
ResponseInfo info = areInvalidArg(key, data, file, token, decodedToken);
235-
if (info != null) {
236-
return info;
237-
}
238-
return FormUploader.upload(client, config, data, file, key, decodedToken, options);
239-
}
240-
241273
}

0 commit comments

Comments
 (0)