Skip to content

Commit 3be2b5b

Browse files
authored
Merge pull request #413 from qiniu/developer
FormUpload With InputStream
2 parents 5565147 + 9e76d5b commit 3be2b5b

File tree

7 files changed

+154
-57
lines changed

7 files changed

+154
-57
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ dependencies {
1616
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
1717
testCompile group: 'com.qiniu', name: 'happy-dns-java', version: '0.1.6'
1818
testCompile group: 'junit', name: 'junit', version: '4.12'
19+
compile group: 'commons-io', name: 'commons-io', version: '2.6'
1920
}
2021

2122

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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.io.IOException;
1010
import java.io.InputStream;
1111

12+
import org.apache.commons.io.IOUtils;
13+
1214
/**
1315
* 七牛文件上传管理器,通过该类上传文件时,会自动根据定义的{@link Configuration#putThreshold}
1416
* 来判断是采用表单上传还是分片上传的方法,超过了定义的{@link Configuration#putThreshold}就会采用
@@ -86,6 +88,37 @@ public void accept(String key, Object value) {
8688
});
8789
return ret;
8890
}
91+
92+
/**
93+
* 上传字节流,表单形式
94+
* 合适小文件,大文件请用流式上传
95+
* @param inputStream
96+
* @param key
97+
* @param token
98+
* @return
99+
* @throws QiniuException
100+
*/
101+
public Response putWithForm(InputStream inputStream, String key, String token) throws QiniuException, IOException {
102+
return putWithForm(inputStream, key, token, null, null, false);
103+
}
104+
105+
/**
106+
* 上传字节流,表单形式
107+
* 合适小文件,大文件请用流式上传
108+
* @param inputStream
109+
* @param key
110+
* @param token
111+
* @param params
112+
* @param mime
113+
* @param checkCrc
114+
* @return
115+
* @throws QiniuException
116+
*/
117+
public Response putWithForm(InputStream inputStream, String key, String token, StringMap params,
118+
String mime, boolean checkCrc) throws QiniuException, IOException {
119+
byte[] data = IOUtils.toByteArray(inputStream);
120+
return put(data, key, token, params, mime, checkCrc);
121+
}
89122

90123
/**
91124
* 上传字节数组

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,4 @@ public static boolean isTravis() {
4343
return "travis".equals(System.getenv("QINIU_TEST_ENV"));
4444
}
4545

46-
public static void main(String[] args) {
47-
try {
48-
System.out.println("done");
49-
} catch (Exception e) {
50-
e.printStackTrace();
51-
}
52-
}
53-
5446
}

src/test/java/test/com/qiniu/rtc/RtcTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
public class RtcTest {
13+
1314
private Auth auth = TestConfig.testAuth;
1415
private RtcAppManager manager = new RtcAppManager(auth);
1516
private RtcRoomManager rmanager = new RtcRoomManager(auth);
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package test.com.qiniu.storage;
2+
3+
import java.io.FileInputStream;
4+
import java.io.InputStream;
5+
6+
import org.junit.Test;
7+
8+
import com.qiniu.common.QiniuException;
9+
import com.qiniu.http.Response;
10+
import com.qiniu.storage.Configuration;
11+
import com.qiniu.storage.UploadManager;
12+
import com.qiniu.util.StringMap;
13+
14+
import test.com.qiniu.TempFile;
15+
import test.com.qiniu.TestConfig;
16+
17+
import static org.junit.Assert.*;
18+
19+
public class FormUploadTest2 {
20+
21+
UploadManager uploadManager = new UploadManager(new Configuration());
22+
23+
/**
24+
* 测试传入inputStream的表单上传
25+
* 检测reqid是否为Null
26+
* 检测状态码是否为200
27+
*/
28+
@Test
29+
public void testFormUploadWithInputStream() {
30+
31+
String token = TestConfig.testAuth.uploadToken(TestConfig.testBucket_z0, TestConfig.testBucket_z0, 3600, null);
32+
System.out.println(token);
33+
34+
try {
35+
InputStream inputStream = new FileInputStream(TempFile.createFile(11));
36+
Response response = uploadManager.putWithForm(inputStream, TestConfig.testBucket_z0, token);
37+
System.out.println(response.reqId);
38+
System.out.println(response.statusCode);
39+
System.out.println(response.bodyString());
40+
assertNotNull(response.reqId);
41+
assertEquals(200, response.statusCode);
42+
} catch (Exception e) {
43+
e.printStackTrace();
44+
}
45+
}
46+
47+
/**
48+
* 测试传入inputStream的表单上传
49+
* 检测reqid是否为Null
50+
* 检测状态码是否为614
51+
*/
52+
@Test
53+
public void testFormUploadWithInputStreamWithPolicy() {
54+
55+
StringMap putPolicy = new StringMap();
56+
putPolicy.put("insertOnly", 1);
57+
String token = TestConfig.testAuth.uploadToken(TestConfig.testBucket_z0, TestConfig.testBucket_z0, 3600, putPolicy);
58+
System.out.println(token);
59+
60+
try {
61+
InputStream inputStream = new FileInputStream(TempFile.createFile(11));
62+
uploadManager.putWithForm(inputStream, TestConfig.testBucket_z0, token);
63+
} catch (Exception e) {
64+
if (e instanceof QiniuException) {
65+
QiniuException ex = (QiniuException) e;
66+
System.out.println(ex.response.reqId);
67+
System.out.println(ex.response.statusCode);
68+
assertNotNull(ex.response.reqId);
69+
assertEquals(614, ex.response.statusCode);
70+
}
71+
}
72+
}
73+
}

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)