本文中的代碼文件在:https://github.com/qiniudemo/qiniu-lab-java/tree/master/src/com/qiniulab/cdn
該參考實現基於七牛的java sdk,能夠從 https//github.com/qiniu/java-sdk 下載。java
代碼以下:git
package com.qiniulab.cdn; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import com.qiniu.common.QiniuException; import com.qiniu.http.Client; import com.qiniu.http.Response; import com.qiniu.util.Auth; import com.qiniu.util.Json; import com.qiniu.util.StringMap; /** * Fusion Cdn 提供的功能 * * <ol> * <li>刷新連接和目錄</li> * <li>預取資源</li> * <li>獲取流量數據</li> * <li>獲取帶寬數據</li> * </ol> */ public class FusionCdn { private Auth auth; public FusionCdn(String accessKey, String secretKey) { this.auth = Auth.create(accessKey, secretKey); } /** * 刷新目錄或者連接列表,注意目錄必須以 / 結尾 * * @param urls * 待刷新的連接列表 * @param dirs * 待刷新的目錄列表 * @throws QiniuException */ public CdnRefreshResult refresh(String[] urls, String[] dirs) throws QiniuException { CdnRefreshResult refreshResult = null; String refreshAPI = "http://fusion.qiniuapi.com/refresh"; Client client = new Client(); StringMap map = new StringMap(); map.put("urls", urls); map.put("dirs", dirs); String postBody = Json.encode(map); String accessToken = String.format("QBox %s", this.auth.signRequest(refreshAPI, null, null)); StringMap headers = new StringMap(); headers.put("Authorization", accessToken); try { Response resp = client.post(refreshAPI, postBody.getBytes("UTF-8"), headers, Client.JsonMime); refreshResult = resp.jsonToObject(CdnRefreshResult.class); } catch (UnsupportedEncodingException e) { } return refreshResult; } /** * 根據資源訪問外鏈進行預取操做,不支持預取目錄 * * @param urls * 待預取的連接列表 * @throws QiniuException */ public CdnPrefetchResult prefetch(String[] urls) throws QiniuException { CdnPrefetchResult prefetchResult = null; String refreshAPI = "http://fusion.qiniuapi.com/prefetch"; Client client = new Client(); StringMap map = new StringMap(); map.put("urls", urls); String postBody = Json.encode(map); String accessToken = String.format("QBox %s", this.auth.signRequest(refreshAPI, null, null)); StringMap headers = new StringMap(); headers.put("Authorization", accessToken); try { Response resp = client.post(refreshAPI, postBody.getBytes("UTF-8"), headers, Client.JsonMime); prefetchResult = resp.jsonToObject(CdnPrefetchResult.class); } catch (UnsupportedEncodingException e) { } return prefetchResult; } /** * @param domain * 須要獲取帶寬的域名 * @param startTime * 起始時間 * @param endTime * 結束時間 */ public DomainBandwidthResult getDomainBandwidth(String domain, String startTime, String endTime) throws QiniuException { DomainBandwidthResult bandwidthResult; String reqUrl = null; try { reqUrl = String.format("http://fusion.qiniuapi.com/domain/bandwidth?domain=%s&startTime=%s&endTime=%s", URLEncoder.encode(domain, "utf-8"), URLEncoder.encode(startTime, "utf-8"), URLEncoder.encode(endTime, "utf-8")); } catch (UnsupportedEncodingException e1) { } Client client = new Client(); StringMap map = new StringMap(); map.put("Content-Type", Client.FormMime); String accessToken = String.format("QBox %s", this.auth.signRequest(reqUrl, null, null)); StringMap headers = new StringMap(); headers.put("Authorization", accessToken); Response resp = client.get(reqUrl, headers); bandwidthResult = resp.jsonToObject(DomainBandwidthResult.class); return bandwidthResult; } /** * @param domain * 須要獲取流量的域名 * @param startTime * 起始時間 * @param endTime * 結束時間 */ public DomainFlowResult getDomainFlow(String domain, String startTime, String endTime) throws QiniuException { DomainFlowResult flowResult; String reqUrl = null; try { reqUrl = String.format("http://fusion.qiniuapi.com/domain/traffic?domain=%s&startTime=%s&endTime=%s", URLEncoder.encode(domain, "utf-8"), URLEncoder.encode(startTime, "utf-8"), URLEncoder.encode(endTime, "utf-8")); } catch (UnsupportedEncodingException e1) { } Client client = new Client(); StringMap map = new StringMap(); map.put("Content-Type", Client.FormMime); String accessToken = String.format("QBox %s", this.auth.signRequest(reqUrl, null, null)); StringMap headers = new StringMap(); headers.put("Authorization", accessToken); Response resp = client.get(reqUrl, headers); flowResult = resp.jsonToObject(DomainFlowResult.class); return flowResult; } }
實例調用:github
刷新目錄或連接golang
// ak,sk從 https://portal.qiniu.com/user/key 獲取 String ak = ""; String sk = ""; FusionCdn cdn = new FusionCdn(ak, sk); // 刷新例子 String[] urlsToRefresh = new String[] { "http://if-pbl.qiniudn.com/golang.png", "http://if-pbl.qiniudn.com/test/golang.png" }; CdnRefreshResult refreshResult; try { refreshResult = cdn.refresh(urlsToRefresh, null); System.out.println(refreshResult.getCode() + "," + refreshResult.getError()); } catch (QiniuException e) { if (e.response != null) { System.err.println(e.response.toString()); } }
資源連接預取json
//ak,sk從 https://portal.qiniu.com/user/key 獲取 String ak = ""; String sk = ""; FusionCdn cdn = new FusionCdn(ak, sk); // 預取例子 String[] urlsToPrefetch = new String[] { "http://if-pbl.qiniudn.com/golang.png", "http://if-pbl.qiniudn.com/test/golang.png" }; CdnPrefetchResult prefetchResult; try { prefetchResult = cdn.prefetch(urlsToPrefetch); System.out.println(prefetchResult.getCode() + "," + prefetchResult.getError()); } catch (QiniuException e) { if (e.response != null) { System.err.println(e.response.toString()); } }
獲取域名帶寬數據api
// ak,sk從 https://portal.qiniu.com/user/key 獲取 String ak = ""; String sk = ""; String domain = "sc.example.com"; String startTime = "2016-06-30 18:00:00"; String endTime = "2016-06-30 20:00:00"; FusionCdn cdn = new FusionCdn(ak, sk); try { DomainBandwidthResult bandwidthResult = cdn.getDomainBandwidth(domain, startTime, endTime); System.out.println(bandwidthResult.getCode()); } catch (QiniuException e) { if (e.response != null) { System.err.println(e.response.toString()); } }
獲取域名流量數據dom
// ak,sk從 https://portal.qiniu.com/user/key 獲取 String ak = ""; String sk = ""; String domain = "sc.example.com"; String startTime = "2016-06-30 18:00:00"; String endTime = "2016-06-30 20:00:00"; FusionCdn cdn = new FusionCdn(ak, sk); try { DomainFlowResult flowResult = cdn.getDomainFlow(domain, startTime, endTime); System.out.println(flowResult.getCode()); } catch (QiniuException e) { if (e.response != null) { System.err.println(e.response.toString()); } }