Skip to content

Commit 0ae9259

Browse files
committed
Merge pull request #93 from qiniu/develop
merge develop to master
2 parents f5bb7e3 + b320af3 commit 0ae9259

File tree

7 files changed

+125
-20
lines changed

7 files changed

+125
-20
lines changed

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
## CHANGE LOG
22

3+
### v6.1.0
4+
5+
2014-1-13 [#93](https://github.com/qiniu/java-sdk/pull/93)
6+
7+
- bugfix: PutExtra.mimeType 不生效问题
8+
- PutPolicy 补充字段
9+
10+
### v6.0.7
11+
12+
2013-11-7 [#85](https://github.com/qiniu/java-sdk/pull/85)
13+
14+
- PutPolicy增加持久化字段
15+
16+
### v6.0.6
17+
18+
2013-11-5 [#84](https://github.com/qiniu/java-sdk/pull/84)
19+
20+
- 修复PutPolicy生成Token时,Expires改变的BUG
21+
322
### v6.0.5
423

524
2013-10-08 issue [#82](https://github.com/qiniu/java-sdk/pull/82)

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
title: Java SDK
33
---
44

5+
# Java SDK 使用指南
56

67
此SDK适用于Java 6及以上版本。基于 [七牛云存储官方API](http://docs.qiniu.com) 构建。使用此 SDK 构建您的网络应用程序,能让您以非常便捷地方式将数据安全地存储到七牛云存储上。无论您的网络应用是一个网站程序,还是包括从云端(服务端程序)到终端(手持设备应用)的架构的服务或应用,通过七牛云存储及其 SDK,都能让您应用程序的终端用户高速上传和下载,同时也让您的服务端更加轻盈。
78

src/main/java/com/qiniu/api/io/IoApi.java

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
package com.qiniu.api.io;
22

33
import java.io.File;
4-
import java.io.InputStream;
54
import java.io.FileInputStream;
5+
import java.io.InputStream;
6+
import java.io.UnsupportedEncodingException;
7+
import java.nio.charset.Charset;
68
import java.util.zip.CRC32;
79
import java.util.zip.CheckedInputStream;
810

911
import org.apache.http.entity.mime.MultipartEntity;
12+
import org.apache.http.entity.mime.content.AbstractContentBody;
1013
import org.apache.http.entity.mime.content.FileBody;
1114
import org.apache.http.entity.mime.content.InputStreamBody;
1215
import org.apache.http.entity.mime.content.StringBody;
16+
1317
import com.qiniu.api.config.Config;
1418
import com.qiniu.api.net.CallRet;
1519
import com.qiniu.api.net.Client;
1620

17-
import java.nio.charset.Charset;
18-
1921
public class IoApi {
2022

21-
public static final String UNDEFINED_KEY = "?";
23+
public static final String UNDEFINED_KEY = null;
2224
public static final int NO_CRC32 = 0;
2325
public static final int AUTO_CRC32 = 1;
2426
public static final int WITH_CRC32 = 2;
@@ -30,15 +32,12 @@ private static PutRet put(String uptoken, String key, File file,
3032
return new PutRet(new CallRet(400, new Exception(
3133
"File does not exist or not readable.")));
3234
}
33-
if (key == null) {
34-
key = UNDEFINED_KEY;
35-
}
3635
MultipartEntity requestEntity = new MultipartEntity();
3736
try {
3837
requestEntity.addPart("token", new StringBody(uptoken));
39-
FileBody fileBody = new FileBody(file);
38+
AbstractContentBody fileBody = buildFileBody(file, extra);
4039
requestEntity.addPart("file", fileBody);
41-
requestEntity.addPart("key", new StringBody(key,Charset.forName("utf-8")));
40+
setKey(requestEntity, key);
4241
if (extra.checkCrc != NO_CRC32) {
4342
if (extra.crc32 == 0) {
4443
return new PutRet(new CallRet(400, new Exception("no crc32 specified!")));
@@ -55,13 +54,27 @@ private static PutRet put(String uptoken, String key, File file,
5554
return new PutRet(ret);
5655
}
5756

57+
private static FileBody buildFileBody(File file,PutExtra extra){
58+
if(extra.mimeType != null){
59+
return new FileBody(file, extra.mimeType);
60+
}else{
61+
return new FileBody(file);
62+
}
63+
}
64+
65+
private static void setKey(MultipartEntity requestEntity, String key) throws UnsupportedEncodingException{
66+
if(key != null){
67+
requestEntity.addPart("key", new StringBody(key,Charset.forName("utf-8")));
68+
}
69+
}
70+
5871
private static PutRet putStream(String uptoken, String key, InputStream reader,PutExtra extra) {
5972
MultipartEntity requestEntity = new MultipartEntity();
6073
try {
6174
requestEntity.addPart("token", new StringBody(uptoken));
62-
InputStreamBody inputBody= new InputStreamBody(reader,key);
75+
AbstractContentBody inputBody = buildInputStreamBody(reader, extra, key);
6376
requestEntity.addPart("file", inputBody);
64-
requestEntity.addPart("key", new StringBody(key,Charset.forName("utf-8")));
77+
setKey(requestEntity, key);
6578
if (extra.checkCrc != NO_CRC32) {
6679
if (extra.crc32 == 0) {
6780
return new PutRet(new CallRet(400, new Exception("no crc32 specified!")));
@@ -78,12 +91,17 @@ private static PutRet putStream(String uptoken, String key, InputStream reader,P
7891
return new PutRet(ret);
7992
}
8093

94+
private static InputStreamBody buildInputStreamBody(InputStream reader,PutExtra extra, String key){
95+
if(extra.mimeType != null){
96+
return new InputStreamBody(reader, extra.mimeType, key);
97+
}else{
98+
return new InputStreamBody(reader, key);
99+
}
100+
}
101+
81102

82103
public static PutRet Put(String uptoken,String key,InputStream reader,PutExtra extra)
83104
{
84-
if (key == null) {
85-
key = UNDEFINED_KEY;
86-
}
87105
PutRet ret = putStream(uptoken,key,reader,extra);
88106
return ret;
89107
}

src/main/java/com/qiniu/api/net/EncodeUtils.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,19 @@ public static byte[] urlsafeEncodeBytes(byte[] src) {
3636
}
3737
return b2;
3838
}
39-
39+
40+
public static byte[] urlsafeBase64Decode(String encoded){
41+
byte[] rawbs = encoded.getBytes();
42+
for(int i=0;i<rawbs.length;i++){
43+
if(rawbs[i] == '_'){
44+
rawbs[i] = '/';
45+
}else if(rawbs[i] == '-'){
46+
rawbs[i] = '+';
47+
}
48+
}
49+
return Base64.decodeBase64(rawbs);
50+
}
51+
4052
public static String urlsafeEncodeString(byte[] src) {
4153
return new String(urlsafeEncodeBytes(src));
4254
}

src/main/java/com/qiniu/api/rs/GetPolicy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ public String makeRequest(String baseUrl, Mac mac) throws AuthException {
1818
if (this.expires == 0) {
1919
this.expires = 3600;
2020
}
21-
this.expires = (int) (System.currentTimeMillis() / 1000 + this.expires);
21+
int expires_ = (int) (System.currentTimeMillis() / 1000 + this.expires);
2222

2323
if (baseUrl.contains("?")) {
2424
baseUrl += "&e=";
2525
} else {
2626
baseUrl += "?e=";
2727
}
28-
baseUrl += this.expires;
28+
baseUrl += expires_;
2929

3030
String downloadToken = DigestAuth.sign(mac, baseUrl.getBytes());
3131
return baseUrl + "&token=" + downloadToken;

src/main/java/com/qiniu/api/rs/PutPolicy.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,26 @@ public class PutPolicy {
3232
public String endUser;
3333
/** 可选 */
3434
public long expires;
35+
/** 可选 */
36+
public String saveKey;
37+
/** 可选。 若非0, 即使Scope为 Bucket:Key 的形式也是insert only*/
38+
public int insertOnly;
39+
/** 可选。若非0, 则服务端根据内容自动确定 MimeType */
40+
public int detectMime;
41+
/** 可选 */
42+
public long fsizeLimit;
43+
/** 可选 */
44+
public String persistentNotifyUrl;
45+
/** 可选 */
46+
public String persistentOps;
47+
48+
public long deadline;
3549

3650
public PutPolicy(String scope) {
3751
this.scope = scope;
3852
}
3953

40-
private String marshal() throws JSONException {
54+
public String marshal() throws JSONException {
4155
JSONStringer stringer = new JSONStringer();
4256
stringer.object();
4357
stringer.key("scope").value(this.scope);
@@ -56,10 +70,28 @@ private String marshal() throws JSONException {
5670
if (this.asyncOps != null && this.asyncOps.length() > 0) {
5771
stringer.key("asyncOps").value(this.asyncOps);
5872
}
73+
if (this.saveKey != null && this.saveKey.length() > 0) {
74+
stringer.key("saveKey").value(this.saveKey);
75+
}
76+
if(this.insertOnly>0){
77+
stringer.key("insertOnly").value(this.insertOnly);
78+
}
79+
if(this.detectMime>0){
80+
stringer.key("detectMime").value(this.detectMime);
81+
}
82+
if(this.fsizeLimit>0){
83+
stringer.key("fsizeLimit").value(this.fsizeLimit);
84+
}
5985
if (this.endUser != null && this.endUser.length() > 0) {
6086
stringer.key("endUser").value(this.endUser);
6187
}
62-
stringer.key("deadline").value(this.expires);
88+
if (this.persistentNotifyUrl != null && this.persistentNotifyUrl.length() > 0) {
89+
stringer.key("persistentNotifyUrl").value(this.persistentNotifyUrl);
90+
}
91+
if (this.persistentOps != null && this.persistentOps.length() > 0) {
92+
stringer.key("persistentOps").value(this.persistentOps);
93+
}
94+
stringer.key("deadline").value(this.deadline);
6395
stringer.endObject();
6496

6597
return stringer.toString();
@@ -78,7 +110,7 @@ public String token(Mac mac) throws AuthException, JSONException {
78110
if (this.expires == 0) {
79111
this.expires = 3600; // 3600s, default.
80112
}
81-
this.expires = System.currentTimeMillis() / 1000 + expires;
113+
this.deadline = System.currentTimeMillis() / 1000 + this.expires;
82114
byte[] data = this.marshal().getBytes();
83115
return DigestAuth.signWithData(mac, data);
84116
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.qiniu.testing;
2+
3+
4+
import junit.framework.TestCase;
5+
6+
import com.qiniu.api.net.EncodeUtils;
7+
8+
public class UtilTest extends TestCase {
9+
10+
// just upload an image in testdata.
11+
public void test() throws Exception {
12+
String expectString = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+}{:?><-=,./;'[]";
13+
String encodedString = "MTIzNDU2Nzg5MGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVp-IUAjJCVeJiooKV8rfXs6Pz48LT0sLi87J1td";
14+
byte[] rawBytes = EncodeUtils.urlsafeBase64Decode(encodedString);
15+
String decoded = new String(rawBytes);
16+
assertTrue(expectString.equals(decoded));
17+
}
18+
19+
@Override
20+
public void tearDown() {
21+
// do nothing here.
22+
}
23+
}

0 commit comments

Comments
 (0)