Skip to content

Commit 360a1c4

Browse files
committed
Merge pull request #107 from simon-liubin/update/charset_utf8
Update/charset utf8
2 parents bba8fb0 + 87b2a88 commit 360a1c4

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

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

Lines changed: 1 addition & 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

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private static PutRet put(String uptoken, String key, File file,
4949

5050
if (extra.params != null) {
5151
for (Map.Entry<String, String> xvar : extra.params.entrySet()) {
52-
requestEntity.addPart(xvar.getKey(), new StringBody(xvar.getValue()));
52+
requestEntity.addPart(xvar.getKey(), new StringBody(xvar.getValue(), Charset.forName(Config.CHARSET)));
5353
}
5454
}
5555
} catch (Exception e) {
@@ -72,7 +72,7 @@ private static FileBody buildFileBody(File file,PutExtra extra){
7272

7373
private static void setKey(MultipartEntity requestEntity, String key) throws UnsupportedEncodingException{
7474
if(key != null){
75-
requestEntity.addPart("key", new StringBody(key,Charset.forName("utf-8")));
75+
requestEntity.addPart("key", new StringBody(key,Charset.forName(Config.CHARSET)));
7676
}
7777
}
7878

@@ -81,7 +81,7 @@ private static void setParam(MultipartEntity requestEntity, Map<String, String>
8181
return;
8282
}
8383
for(String name : params.keySet()){
84-
requestEntity.addPart(name, new StringBody(params.get(name),Charset.forName("utf-8")));
84+
requestEntity.addPart(name, new StringBody(params.get(name),Charset.forName(Config.CHARSET)));
8585
}
8686
}
8787

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/rs/PutPolicy.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.qiniu.api.auth.AuthException;
77
import com.qiniu.api.auth.digest.DigestAuth;
88
import com.qiniu.api.auth.digest.Mac;
9+
import com.qiniu.api.net.EncodeUtils;
910

1011
/**
1112
* The PutPolicy class used to generate a upload token. To upload a file, you
@@ -146,7 +147,7 @@ public String token(Mac mac) throws AuthException, JSONException {
146147
this.expires = 3600; // 3600s, default.
147148
}
148149
this.deadline = System.currentTimeMillis() / 1000 + this.expires;
149-
byte[] data = this.marshal().getBytes();
150+
byte[] data = EncodeUtils.toByte(this.marshal());
150151
return DigestAuth.signWithData(mac, data);
151152
}
152153

0 commit comments

Comments
 (0)