Skip to content

Commit f7eb77d

Browse files
authored
Merge pull request #416 from qiniu/developer
FormUpload With InputStream
2 parents a1101eb + da89d79 commit f7eb77d

File tree

6 files changed

+144
-49
lines changed

6 files changed

+144
-49
lines changed

src/main/java/com/qiniu/common/Region.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public String getApiHost() {
5858
/**
5959
* 域名构造器
6060
*/
61-
static class Builder {
61+
public static class Builder {
6262
protected Region region;
6363

6464
public Builder() {

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.qiniu.common.QiniuException;
44
import com.qiniu.http.Client;
55
import com.qiniu.http.Response;
6+
import com.qiniu.util.IOUtils;
67
import com.qiniu.util.StringMap;
78

89
import java.io.File;
@@ -86,6 +87,28 @@ public void accept(String key, Object value) {
8687
});
8788
return ret;
8889
}
90+
91+
/**
92+
* 上传字节流,小文件走表单,大文件走分片
93+
* @param inputStream
94+
* @param size
95+
* @param key
96+
* @param token
97+
* @param params
98+
* @param mime
99+
* @param checkCrc
100+
* @return
101+
* @throws QiniuException
102+
* @throws IOException
103+
*/
104+
public Response put(InputStream inputStream, long size, String key, String token, StringMap params,
105+
String mime, boolean checkCrc) throws QiniuException, IOException {
106+
if (size < 0 || size > configuration.putThreshold) {
107+
return put(inputStream, key, token, params, mime);
108+
}
109+
byte[] data = IOUtils.toByteArray(inputStream);
110+
return put(data, key, token, params, mime, checkCrc);
111+
}
89112

90113
/**
91114
* 上传字节数组
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.qiniu.util;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.ByteArrayOutputStream;
6+
7+
public class IOUtils {
8+
9+
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
10+
11+
/**
12+
* 输入InputSteam,返回byte[].
13+
* 参考:https://github.com/apache/commons-io/blob/master/src/main/java/org/apache/commons/io/IOUtils.java<br>
14+
* @param input
15+
* @return
16+
* @throws IOException
17+
*/
18+
public static byte[] toByteArray(final InputStream input) throws IOException {
19+
try (final ByteArrayOutputStream output = new ByteArrayOutputStream()) {
20+
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
21+
int n;
22+
while (-1 != (n = input.read(buffer))) {
23+
output.write(buffer, 0, n);
24+
}
25+
return output.toByteArray();
26+
}
27+
}
28+
29+
}

src/test/java/test/com/qiniu/TestConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.qiniu.util.Auth;
44

5+
import test.com.qiniu.storage.FormUploadTest;
6+
57
public final class TestConfig {
68

79
//dummy: ak, sk, ...
@@ -45,6 +47,8 @@ public static boolean isTravis() {
4547

4648
public static void main(String[] args) {
4749
try {
50+
FormUploadTest t = new FormUploadTest();
51+
t.testFormUploadWithInputStream();
4852
System.out.println("done");
4953
} catch (Exception e) {
5054
e.printStackTrace();

src/test/java/test/com/qiniu/storage/FormUploadTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.io.File;
1515
import java.io.FileInputStream;
1616
import java.io.IOException;
17+
import java.io.InputStream;
1718
import java.util.HashMap;
1819
import java.util.Map;
1920
import java.util.concurrent.CountDownLatch;
@@ -450,6 +451,47 @@ public void testFormLargeSize2() {
450451
}
451452
}
452453
}
454+
455+
/**
456+
* 测试inputStream 表单上传
457+
* 检测reqid是否为Null
458+
* 检测状态码是否为200
459+
*/
460+
@Test
461+
public void testFormUploadWithInputStream() {
462+
testFormUploadWithInputStream(1, -1);
463+
testFormUploadWithInputStream(1, 0);
464+
testFormUploadWithInputStream(1, 1000);
465+
testFormUploadWithInputStream(4 * 1024, 4 * 1024 * 1024);
466+
testFormUploadWithInputStream(5 * 1024, -1);
467+
testFormUploadWithInputStream(5 * 1024, 5 * 1024 * 1024);
468+
}
469+
470+
/**
471+
* 测试inputStream 表单上传
472+
* 检测reqid是否为Null
473+
* 检测状态码是否为200
474+
*/
475+
public void testFormUploadWithInputStream(long kiloSize, long size) {
476+
477+
String token = TestConfig.testAuth.uploadToken(TestConfig.testBucket_z0, TestConfig.testBucket_z0, 3600, null);
478+
System.out.println("token="+token);
479+
480+
try {
481+
File file = TempFile.createFile(kiloSize);
482+
InputStream inputStream = new FileInputStream(file);
483+
System.out.println("length=" + file.length());
484+
System.out.println("size=" + size);
485+
Response response = uploadManager.put(inputStream, size, TestConfig.testBucket_z0, token, null, null, false);
486+
System.out.println("code="+response.statusCode);
487+
System.out.println("reqid="+response.reqId);
488+
System.out.println(response.bodyString());
489+
assertNotNull(response.reqId);
490+
assertEquals(200, response.statusCode);
491+
} catch (Exception e) {
492+
e.printStackTrace();
493+
}
494+
}
453495

454496
class MyRet {
455497
public String hash;

src/test/java/test/com/qiniu/streaming/StreamingTest.java

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,97 @@
11
package test.com.qiniu.streaming;
22

3-
import com.google.gson.JsonSyntaxException;
43
import com.qiniu.common.QiniuException;
54
import com.qiniu.streaming.StreamingManager;
65
import com.qiniu.streaming.model.ActivityRecords;
76
import com.qiniu.streaming.model.StreamAttribute;
87
import com.qiniu.streaming.model.StreamListing;
9-
import com.qiniu.streaming.model.StreamStatus;
108
import com.qiniu.util.Auth;
11-
import com.qiniu.util.Json;
129
import org.junit.Test;
1310
import test.com.qiniu.TestConfig;
1411

1512
import static org.junit.Assert.*;
1613

1714
/**
1815
* Created by bailong on 16/9/22
16+
* Updated by panyuan on 19/3/12
1917
*/
2018
public class StreamingTest {
19+
2120
private Auth auth = TestConfig.testAuth;
22-
2321
private String hub = "pilisdktest";
24-
private String streamKeyPrefix = "pilijava" + System.currentTimeMillis();
22+
private String stream = "javasdk";
23+
private String streamNoExist = "javasdk" + "NoExist";
24+
private String streamKeyPrefix = "javasdk";
2525
private StreamingManager manager = new StreamingManager(auth, hub);
2626

27-
28-
//@Test
27+
/**
28+
* 测试获取不存在的流的信息
29+
* 检测返回状态码是否是612
30+
*/
31+
@Test
2932
public void testGetNoExistStream() {
3033
try {
31-
manager.attribute("nnnoexist");
34+
manager.attribute(streamNoExist);
3235
fail("should not exist");
3336
} catch (QiniuException e) {
34-
e.printStackTrace();
3537
assertEquals(612, e.code());
3638
}
3739
}
3840

39-
// CHECKSTYLE:OFF
40-
//@Test
41+
/**
42+
* 测试创建、禁用、启用、获取流信息、列举
43+
* @throws QiniuException
44+
*/
45+
@Test
4146
public void testStreamOperation() throws QiniuException {
42-
// CHECKSTYLE:ON
43-
String streamKey = streamKeyPrefix + "-a";
44-
45-
manager.create(streamKey);
46-
47-
StreamAttribute attr = manager.attribute(streamKey);
47+
StreamAttribute attr = manager.attribute(stream);
4848
assertEquals(0, attr.disabledTill);
4949
assertNotEquals(0, attr.createdAt);
5050

5151
try {
52-
manager.create(streamKey);
52+
manager.create(stream);
5353
fail("has already existed");
5454
} catch (QiniuException e) {
5555
assertEquals(614, e.code());
5656
}
5757

58-
manager.disableTill(streamKey, -1);
58+
manager.disableTill(stream, -1);
5959

60-
attr = manager.attribute(streamKey);
60+
attr = manager.attribute(stream);
6161
assertEquals(-1, attr.disabledTill);
6262
assertNotEquals(0, attr.updatedAt);
6363

64-
manager.enable(streamKey);
65-
attr = manager.attribute(streamKey);
64+
manager.enable(stream);
65+
attr = manager.attribute(stream);
6666
assertEquals(0, attr.disabledTill);
6767
assertNotEquals(0, attr.updatedAt);
6868

6969
long t = System.currentTimeMillis() / 1000 + 3600;
70-
manager.disableTill(streamKey, t);
71-
attr = manager.attribute(streamKey);
70+
manager.disableTill(stream, t);
71+
attr = manager.attribute(stream);
7272
assertEquals(t, attr.disabledTill);
7373
assertNotEquals(0, attr.updatedAt);
7474

75-
manager.enable(streamKey);
76-
attr = manager.attribute(streamKey);
75+
manager.enable(stream);
76+
attr = manager.attribute(stream);
7777
assertEquals(0, attr.disabledTill);
7878
assertNotEquals(0, attr.updatedAt);
7979

8080
try {
81-
StreamStatus status = manager.status(streamKey);
81+
manager.status(stream);
8282
fail();
8383
} catch (QiniuException e) {
8484
assertEquals(619, e.code());
8585
}
8686

8787
try {
88-
manager.saveAs(streamKey, null, 0, 0);
88+
manager.saveAs(stream, null, 0, 0);
8989
fail();
9090
} catch (QiniuException e) {
9191
assertEquals(619, e.code());
9292
}
9393

94-
ActivityRecords records = manager.history(streamKey, System.currentTimeMillis() / 1000 - 1000, 0);
94+
ActivityRecords records = manager.history(stream, System.currentTimeMillis() / 1000 - 1000, 0);
9595
assertEquals(0, records.items.length);
9696

9797
StreamListing l = manager.listStreams(false, streamKeyPrefix, null);
@@ -118,34 +118,31 @@ public void testStreamOperation() throws QiniuException {
118118
assertFalse(it.hasNext());
119119
}
120120

121-
//@Test
121+
/**
122+
* 测试saveas
123+
* 检测返回状态码是否是404
124+
* @throws QiniuException
125+
*/
126+
@Test
122127
public void testSaveAs() throws QiniuException {
123128
try {
124-
manager.saveAs("test--sd", "f\"ff.m3u8");
129+
manager.saveAs(streamNoExist, "f\"ff.m3u8");
125130
} catch (QiniuException e) {
126-
// 619 , no data; 612 stream not found, 但请求正常 //
127-
if (e.code() != 619 && e.code() != 612) {
128-
throw e;
129-
}
131+
assertEquals(404, e.response.statusCode);
130132
}
131133
}
132134

133-
//@Test
135+
/**
136+
* 测试创建流
137+
* 检测返回状态码是否为614
138+
* @throws QiniuException
139+
*/
140+
@Test
134141
public void testCreate() throws QiniuException {
135142
try {
136-
String body = String.format("{\"key\":\"%s\"}", "stream\"Key");
137-
System.out.println(body);
138-
Json.decode(body);
139-
fail("json 解析不正确");
140-
} catch (JsonSyntaxException e) {
141-
142-
}
143-
try {
144-
manager.create("streamKey");
143+
manager.create(stream);
145144
} catch (QiniuException e) {
146-
if (e.code() != 614) {
147-
throw e;
148-
}
145+
assertEquals(614, e.code());
149146
}
150147
}
151148

0 commit comments

Comments
 (0)