Skip to content

Commit e798c2f

Browse files
committed
refractor the function of create anti leech url
1 parent d6f284c commit e798c2f

File tree

2 files changed

+75
-21
lines changed

2 files changed

+75
-21
lines changed

src/main/java/com/qiniu/cdn/CdnManager.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
import java.net.URL;
1515
import java.net.URLEncoder;
1616
import java.security.NoSuchAlgorithmException;
17+
import java.util.Map;
18+
import java.util.ArrayList;
1719
import java.util.HashMap;
20+
import java.util.List;
1821

1922
/**
2023
* Created by bailong on 16/9/21.
@@ -139,20 +142,28 @@ public Response getCdnLogList(String[] domains, String logDate) throws QiniuExce
139142
/**
140143
* 构建标准的基于时间戳的防盗链
141144
*
142-
* @param host 自定义域名,例如 http://img.abc.com
143-
* @param fileName 待访问的原始文件名,必须是utf8编码,不需要进行urlencode
144-
* @param queryString 业务自身的查询参数,必须是utf8编码,不需要进行urlencode
145-
* @param encryptKey 时间戳防盗链的签名密钥,从七牛后台获取
146-
* @param deadline 链接的有效期时间戳,是以秒为单位的Unix时间戳
147-
* @return signedUrl 最终的带时间戳防盗链的url
145+
* @param host 自定义域名,例如 http://img.abc.com
146+
* @param fileName 待访问的原始文件名,必须是utf8编码,不需要进行urlencode
147+
* @param queryStringMap 业务自身的查询参数,必须是utf8编码,不需要进行urlencode
148+
* @param encryptKey 时间戳防盗链的签名密钥,从七牛后台获取
149+
* @param deadline 链接的有效期时间戳,是以秒为单位的Unix时间戳
150+
* @return signedUrl 最终的带时间戳防盗链的url
148151
*/
149152
public static String createTimestampAntiLeechUrl(
150-
String host, String fileName, String queryString, String encryptKey, long deadline)
153+
String host, String fileName, final StringMap queryStringMap, String encryptKey, long deadline)
151154
throws UnsupportedEncodingException, MalformedURLException, NoSuchAlgorithmException {
152155
String urlToSign;
153-
if (queryString.trim().length() != 0) {
156+
if (queryStringMap != null && queryStringMap.size() > 0) {
157+
List<String> queryStrings = new ArrayList<String>();
158+
for (Map.Entry<String, Object> entry : queryStringMap.map().entrySet()) {
159+
StringBuilder queryStringBuilder = new StringBuilder();
160+
queryStringBuilder.append(URLEncoder.encode(entry.getKey(), "utf-8"));
161+
queryStringBuilder.append("=");
162+
queryStringBuilder.append(URLEncoder.encode(entry.getValue().toString(), "utf-8"));
163+
queryStrings.add(queryStringBuilder.toString());
164+
}
154165
urlToSign = String.format("%s/%s?%s", host, URLEncoder.encode(fileName, "utf-8"),
155-
URLEncoder.encode(queryString, "utf-8"));
166+
StringUtils.join(queryStrings, "&"));
156167
} else {
157168
urlToSign = String.format("%s/%s", host, URLEncoder.encode(fileName, "utf-8"));
158169
}

src/test/java/com/qiniu/CdnTest.java

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.qiniu.cdn.CdnManager;
44
import com.qiniu.common.QiniuException;
55
import com.qiniu.http.Response;
6+
import com.qiniu.util.StringMap;
67
import org.junit.Assert;
78
import org.junit.Test;
89

@@ -87,16 +88,55 @@ public void testGetCdnLogList() {
8788
}
8889

8990
@Test
90-
public void testCreateTimestampAntiLeechUrlSimple() {
91-
String host = "http://img.abc.com";
92-
String fileName = "2017/01/07/测试.png";
93-
String queryString = "";
91+
public void testCreateTimestampAntiLeechUrlSimple1() {
92+
String host = "http://video.example.com";
93+
String fileName = "2017/01/07/test.png";
94+
95+
long deadline = System.currentTimeMillis() / 1000 + 3600;
96+
String encryptKey = "xxx";
97+
String signedUrl;
98+
try {
99+
signedUrl = CdnManager.createTimestampAntiLeechUrl(host, fileName,
100+
null, encryptKey, deadline);
101+
System.out.println(signedUrl);
102+
} catch (Exception ex) {
103+
ex.printStackTrace();
104+
Assert.fail();
105+
}
106+
}
107+
108+
@Test
109+
public void testCreateTimestampAntiLeechUrlSimple2() {
110+
String host = "http://video.example.com";
111+
String fileName = "基本概括.mp4";
112+
long deadline = System.currentTimeMillis() / 1000 + 3600;
113+
String encryptKey = "xxx";
114+
String signedUrl;
115+
try {
116+
signedUrl = CdnManager.createTimestampAntiLeechUrl(host, fileName,
117+
null, encryptKey, deadline);
118+
System.out.println(signedUrl);
119+
} catch (Exception ex) {
120+
ex.printStackTrace();
121+
Assert.fail();
122+
}
123+
}
124+
125+
126+
@Test
127+
public void testCreateTimestampAntiLeechUrlWithQueryString1() {
128+
String host = "http://video.example.com";
129+
String fileName = "2017/01/07/test.png";
130+
StringMap queryStringMap = new StringMap();
131+
queryStringMap.put("name", "七牛");
132+
queryStringMap.put("year", 2017);
133+
queryStringMap.put("年龄", 28);
94134
long deadline = System.currentTimeMillis() / 1000 + 3600;
95-
String encryptKey = "";
135+
String encryptKey = "xxx";
96136
String signedUrl;
97137
try {
98138
signedUrl = CdnManager.createTimestampAntiLeechUrl(host, fileName,
99-
queryString, encryptKey, deadline);
139+
queryStringMap, encryptKey, deadline);
100140
System.out.println(signedUrl);
101141
} catch (Exception ex) {
102142
ex.printStackTrace();
@@ -105,16 +145,19 @@ public void testCreateTimestampAntiLeechUrlSimple() {
105145
}
106146

107147
@Test
108-
public void testCreateTimestampAntiLeechUrlWithQueryString() {
109-
String host = "http://video.abc.com";
110-
String fileName = "测试.mp4";
111-
String queryString = "name=七牛&year=2017";
148+
public void testCreateTimestampAntiLeechUrlWithQueryString2() {
149+
String host = "http://video.example.com";
150+
String fileName = "基本概括.mp4";
151+
StringMap queryStringMap = new StringMap();
152+
queryStringMap.put("name", "七牛");
153+
queryStringMap.put("year", 2017);
154+
queryStringMap.put("年龄", 28);
112155
long deadline = System.currentTimeMillis() / 1000 + 3600;
113-
String encryptKey = "";
156+
String encryptKey = "xxx";
114157
String signedUrl;
115158
try {
116159
signedUrl = CdnManager.createTimestampAntiLeechUrl(host, fileName,
117-
queryString, encryptKey, deadline);
160+
queryStringMap, encryptKey, deadline);
118161
System.out.println(signedUrl);
119162
} catch (Exception ex) {
120163
ex.printStackTrace();

0 commit comments

Comments
 (0)