Skip to content

Commit 68864b5

Browse files
committed
Merge pull request #109 from qiniu/develop
Release 6.1.3
2 parents d79d0d2 + c961af0 commit 68864b5

25 files changed

+1431
-78
lines changed

.classpath

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
<attribute name="maven.pomderived" value="true"/>
1313
</attributes>
1414
</classpathentry>
15-
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
16-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
15+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
1716
<attributes>
1817
<attribute name="maven.pomderived" value="true"/>
1918
</attributes>

.settings/org.eclipse.jdt.core.prefs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
eclipse.preferences.version=1
2-
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
3-
org.eclipse.jdt.core.compiler.compliance=1.6
42
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5-
org.eclipse.jdt.core.compiler.source=1.6

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
## CHANGE LOG
22

3+
### v6.1.3
4+
5+
2014-04-28 [#109](https://github.com/qiniu/java-sdk/pull/109)
6+
7+
- [#101] [#102] 用户自定义参数
8+
- [#103] 分片上传,断点续传
9+
- [#106] bugfix: 普通流上传,key为空对象导致错误
10+
- [#107] bugfix: 指明为utf8字符集
11+
- [#108] 限定上传文件类型
12+
313
### v6.1.2
414

515
2014-2-10 [#98](https://github.com/qiniu/java-sdk/pull/98)
@@ -13,7 +23,7 @@
1323
- bugfix: PutExtra.mimeType 不生效问题
1424
- PutPolicy 补充字段
1525

16-
### v6.0.7
26+
### v6.0.7
1727

1828
2013-11-7 [#85](https://github.com/qiniu/java-sdk/pull/85)
1929

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Java SDK
2+
title: Java SDK | 七牛云存储
33
---
44

55
# Java SDK 使用指南

src/main/java/com/qiniu/api/auth/digest/Mac.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public Mac(String accessKey, String secretKey) {
2929
* @throws AuthException
3030
*/
3131
public String sign(byte[] data) throws AuthException {
32-
System.out.println("data : " + new String(data));
3332
javax.crypto.Mac mac = null;
3433
try {
3534
mac = javax.crypto.Mac.getInstance("HmacSHA1");

src/main/java/com/qiniu/api/config/Config.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* side only.
66
*/
77
public class Config {
8+
public static final String CHARSET = "utf-8";
89

910
public static String USER_AGENT="qiniu java-sdk v6.0.0";
1011

@@ -25,5 +26,24 @@ public class Config {
2526
public static String UP_HOST = "http://up.qbox.me";
2627

2728
public static String RSF_HOST = "http://rsf.qbox.me";
29+
30+
/**
31+
* HTTP连接超时的时间毫秒(ms)
32+
* Determines the timeout in milliseconds until a connection is established.
33+
* A timeout value of zero is interpreted as an infinite timeout.
34+
*
35+
* Please note this parameter can only be applied to connections that
36+
* are bound to a particular local address.
37+
*/
38+
public static int CONNECTION_TIMEOUT = 30 * 1000;
39+
/**
40+
* 读取response超时的时间毫秒(ms)
41+
* Defines the socket timeout (<code>SO_TIMEOUT</code>) in milliseconds,
42+
* which is the timeout for waiting for data or, put differently,
43+
* a maximum period inactivity between two consecutive data packets).
44+
* A timeout value of zero is interpreted as an infinite timeout.
45+
* @see java.net.SocketOptions#SO_TIMEOUT
46+
*/
47+
public static int SO_TIMEOUT = 30 * 1000;
2848

2949
}

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

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.InputStream;
66
import java.io.UnsupportedEncodingException;
77
import java.nio.charset.Charset;
8+
import java.util.Map;
89
import java.util.zip.CRC32;
910
import java.util.zip.CheckedInputStream;
1011

@@ -38,12 +39,19 @@ private static PutRet put(String uptoken, String key, File file,
3839
AbstractContentBody fileBody = buildFileBody(file, extra);
3940
requestEntity.addPart("file", fileBody);
4041
setKey(requestEntity, key);
42+
setParam(requestEntity, extra.params);
4143
if (extra.checkCrc != NO_CRC32) {
4244
if (extra.crc32 == 0) {
4345
return new PutRet(new CallRet(400, new Exception("no crc32 specified!")));
4446
}
4547
requestEntity.addPart("crc32", new StringBody(extra.crc32 + ""));
46-
}
48+
}
49+
50+
if (extra.params != null) {
51+
for (Map.Entry<String, String> xvar : extra.params.entrySet()) {
52+
requestEntity.addPart(xvar.getKey(), new StringBody(xvar.getValue(), Charset.forName(Config.CHARSET)));
53+
}
54+
}
4755
} catch (Exception e) {
4856
e.printStackTrace();
4957
return new PutRet(new CallRet(400, e));
@@ -64,17 +72,27 @@ private static FileBody buildFileBody(File file,PutExtra extra){
6472

6573
private static void setKey(MultipartEntity requestEntity, String key) throws UnsupportedEncodingException{
6674
if(key != null){
67-
requestEntity.addPart("key", new StringBody(key,Charset.forName("utf-8")));
75+
requestEntity.addPart("key", new StringBody(key,Charset.forName(Config.CHARSET)));
76+
}
77+
}
78+
79+
private static void setParam(MultipartEntity requestEntity, Map<String, String> params) throws UnsupportedEncodingException{
80+
if(params == null){
81+
return;
82+
}
83+
for(String name : params.keySet()){
84+
requestEntity.addPart(name, new StringBody(params.get(name),Charset.forName(Config.CHARSET)));
6885
}
6986
}
7087

71-
private static PutRet putStream(String uptoken, String key, InputStream reader,PutExtra extra) {
88+
private static PutRet putStream(String uptoken, String key, InputStream reader,PutExtra extra, String fileName) {
7289
MultipartEntity requestEntity = new MultipartEntity();
7390
try {
7491
requestEntity.addPart("token", new StringBody(uptoken));
75-
AbstractContentBody inputBody = buildInputStreamBody(reader, extra, key);
92+
AbstractContentBody inputBody = buildInputStreamBody(reader, extra, fileName != null ? fileName : "null");
7693
requestEntity.addPart("file", inputBody);
7794
setKey(requestEntity, key);
95+
setParam(requestEntity, extra.params);
7896
if (extra.checkCrc != NO_CRC32) {
7997
if (extra.crc32 == 0) {
8098
return new PutRet(new CallRet(400, new Exception("no crc32 specified!")));
@@ -91,19 +109,26 @@ private static PutRet putStream(String uptoken, String key, InputStream reader,P
91109
return new PutRet(ret);
92110
}
93111

94-
private static InputStreamBody buildInputStreamBody(InputStream reader,PutExtra extra, String key){
112+
private static InputStreamBody buildInputStreamBody(InputStream reader,PutExtra extra, String fileName){
95113
if(extra.mimeType != null){
96-
return new InputStreamBody(reader, extra.mimeType, key);
114+
return new InputStreamBody(reader, extra.mimeType, fileName);
97115
}else{
98-
return new InputStreamBody(reader, key);
116+
return new InputStreamBody(reader, fileName);
99117
}
100118
}
101119

120+
public static PutRet put(String uptoken,String key,InputStream reader,PutExtra extra){
121+
return putStream(uptoken,key,reader,extra, null);
122+
}
123+
124+
public static PutRet put(String uptoken,String key,InputStream reader,PutExtra extra, String fileName){
125+
return putStream(uptoken,key,reader,extra, fileName);
126+
}
127+
102128

103129
public static PutRet Put(String uptoken,String key,InputStream reader,PutExtra extra)
104130
{
105-
PutRet ret = putStream(uptoken,key,reader,extra);
106-
return ret;
131+
return put(uptoken,key,reader,extra);
107132
}
108133

109134

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.qiniu.api.net;
22

3+
import java.io.UnsupportedEncodingException;
34
import java.util.ArrayList;
45
import java.util.HashMap;
56
import java.util.Map;
@@ -10,6 +11,8 @@
1011
import org.apache.http.client.utils.URLEncodedUtils;
1112
import org.apache.http.message.BasicNameValuePair;
1213

14+
import com.qiniu.api.config.Config;
15+
1316
/**
1417
* URLEncoding is the alternate base64 encoding defined in RFC 4648. It is
1518
* typically used in URLs and file names.
@@ -38,7 +41,7 @@ public static byte[] urlsafeEncodeBytes(byte[] src) {
3841
}
3942

4043
public static byte[] urlsafeBase64Decode(String encoded){
41-
byte[] rawbs = encoded.getBytes();
44+
byte[] rawbs = toByte(encoded);
4245
for(int i=0;i<rawbs.length;i++){
4346
if(rawbs[i] == '_'){
4447
rawbs[i] = '/';
@@ -50,11 +53,11 @@ public static byte[] urlsafeBase64Decode(String encoded){
5053
}
5154

5255
public static String urlsafeEncodeString(byte[] src) {
53-
return new String(urlsafeEncodeBytes(src));
56+
return toString(urlsafeEncodeBytes(src));
5457
}
5558

5659
public static String urlsafeEncode(String text) {
57-
return new String(urlsafeEncodeBytes(text.getBytes()));
60+
return toString(urlsafeEncodeBytes(toByte(text)));
5861
}
5962

6063
// replace '/' with '_', '+" with '-'
@@ -84,8 +87,24 @@ public static String encodeParams(Object params) {
8487
list.add(new BasicNameValuePair(entry.getKey(), entry
8588
.getValue()));
8689
}
87-
return URLEncodedUtils.format(list, "UTF-8");
90+
return URLEncodedUtils.format(list, Config.CHARSET);
8891
}
8992
return null;
9093
}
94+
95+
public static byte[] toByte(String s){
96+
try {
97+
return s.getBytes(Config.CHARSET);
98+
} catch (UnsupportedEncodingException e) {
99+
throw new RuntimeException(e);
100+
}
101+
}
102+
103+
public static String toString(byte[] bs){
104+
try {
105+
return new String(bs, Config.CHARSET);
106+
} catch (UnsupportedEncodingException e) {
107+
throw new RuntimeException(e);
108+
}
109+
}
91110
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import org.apache.http.conn.ssl.SSLSocketFactory;
1010
import org.apache.http.impl.client.DefaultHttpClient;
1111
import org.apache.http.impl.conn.PoolingClientConnectionManager;
12+
import org.apache.http.params.CoreConnectionPNames;
13+
14+
import com.qiniu.api.config.Config;
1215

1316
/**
1417
* The class Http provides a default HttpConnectionPool implementation. Anyway,
@@ -54,6 +57,10 @@ private static HttpClient makeDefaultClient() {
5457
HttpHost localhost = new HttpHost("locahost", 80);
5558
cm.setMaxPerRoute(new HttpRoute(localhost), 50);
5659

57-
return new DefaultHttpClient(cm);
60+
HttpClient client = new DefaultHttpClient(cm);
61+
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, Config.CONNECTION_TIMEOUT);
62+
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, Config.SO_TIMEOUT);
63+
64+
return client;
5865
}
5966
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.qiniu.api.resumableio;
2+
3+
import org.json.JSONException;
4+
import org.json.JSONObject;
5+
6+
import com.qiniu.api.net.CallRet;
7+
8+
public class ChunkUploadCallRet extends CallRet {
9+
protected String ctx;
10+
protected String checksum;
11+
protected int offset;
12+
protected String host;
13+
protected long crc32;
14+
15+
public ChunkUploadCallRet(CallRet ret) {
16+
super(ret);
17+
doUnmarshal();
18+
}
19+
20+
public ChunkUploadCallRet(int statusCode, String response) {
21+
super(statusCode, response);
22+
doUnmarshal();
23+
}
24+
25+
public ChunkUploadCallRet(int statusCode, Exception e) {
26+
super(statusCode, e);
27+
}
28+
29+
private void doUnmarshal() {
30+
if (this.exception != null || this.response == null
31+
|| !this.response.trim().startsWith("{")) {
32+
return;
33+
}
34+
try {
35+
unmarshal();
36+
} catch (Exception e) {
37+
e.printStackTrace();
38+
if (this.exception == null) {
39+
this.exception = e;
40+
}
41+
}
42+
43+
}
44+
45+
protected void unmarshal() throws JSONException{
46+
JSONObject jsonObject = new JSONObject(this.response);
47+
ctx = jsonObject.optString("ctx", null);
48+
checksum = jsonObject.optString("checksum", null);
49+
offset = jsonObject.optInt("offset", 0);
50+
host = jsonObject.optString("host", null);
51+
crc32 = jsonObject.optLong("crc32", 0);
52+
}
53+
54+
public String getCtx() {
55+
return ctx;
56+
}
57+
58+
public String getChecksum() {
59+
return checksum;
60+
}
61+
62+
public long getOffset() {
63+
return offset;
64+
}
65+
66+
public String getHost() {
67+
return host;
68+
}
69+
70+
public long getCrc32() {
71+
return crc32;
72+
}
73+
74+
}

0 commit comments

Comments
 (0)