Commit ddfaec280d85254bdec9420b01446797e81ca393

Authored by 张宗朋
2 parents 266be006 5a21e77e

Merge branch 'zzpUGC' into 'master'

baseUtil

ok

See merge request !10
src/main/java/com/cnlive/mz/live/rest/BaseUtil.java
@@ -26,8 +26,9 @@ import sun.misc.BASE64Encoder; @@ -26,8 +26,9 @@ import sun.misc.BASE64Encoder;
26 public class BaseUtil { 26 public class BaseUtil {
27 27
28 private static final String ENCODING = "UTF-8"; 28 private static final String ENCODING = "UTF-8";
29 - private static final String AK_SECRET = "7SxVU5lFpqCEQIqxLprQat9BWoyZNKlKfSScTuIk";  
30 - private static final String AK_ID = "I3TC8a4xboPUMe5xlp22"; 29 + // AccessKey、SecretKey
  30 + private static final String SECRET_KEY = "7SxVU5lFpqCEQIqxLprQat9BWoyZNKlKfSScTuIk";
  31 + private static final String ACCESS_KEY = "I3TC8a4xboPUMe5xlp22";
31 32
32 /* 33 /*
33 * 计算MD5+BASE64 34 * 计算MD5+BASE64
@@ -60,9 +61,7 @@ public class BaseUtil { @@ -60,9 +61,7 @@ public class BaseUtil {
60 Mac mac = Mac.getInstance("HmacSHA1"); 61 Mac mac = Mac.getInstance("HmacSHA1");
61 mac.init(signingKey); 62 mac.init(signingKey);
62 byte[] rawHmac = mac.doFinal(data.getBytes()); 63 byte[] rawHmac = mac.doFinal(data.getBytes());
63 - //result = new String(Base64.encodeBase64(rawHmac, false), ENCODING);  
64 - //result = (new BASE64Encoder()).encode(rawHmac);  
65 - result = new String(Base64.encodeBase64(rawHmac),ENCODING); 64 + result = new String(Base64.encodeBase64(rawHmac), ENCODING);
66 } catch (Exception e) { 65 } catch (Exception e) {
67 throw new Error("Failed to generate HMAC : " + e.getMessage()); 66 throw new Error("Failed to generate HMAC : " + e.getMessage());
68 } 67 }
@@ -71,54 +70,51 @@ public class BaseUtil { @@ -71,54 +70,51 @@ public class BaseUtil {
71 70
72 /** 71 /**
73 * 获取当前时间戳 72 * 获取当前时间戳
  73 + *
74 * @return 74 * @return
75 */ 75 */
76 - public static String getTimestamp(){ 76 + public static String getTimestamp() {
77 DateFormat df = new SimpleDateFormat("yyyyMMdd"); 77 DateFormat df = new SimpleDateFormat("yyyyMMdd");
78 Date date = null; 78 Date date = null;
79 try { 79 try {
80 - date = df.parse("20160607"); 80 + Calendar calendar = Calendar.getInstance();
  81 + calendar.add(Calendar.DATE, +1);
  82 + SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
  83 + String motoday = sdf.format(calendar.getTime());
  84 + date = df.parse(motoday);
81 } catch (ParseException e) { 85 } catch (ParseException e) {
82 e.printStackTrace(); 86 e.printStackTrace();
83 } 87 }
84 Calendar cal = Calendar.getInstance(); 88 Calendar cal = Calendar.getInstance();
85 cal.setTime(date); 89 cal.setTime(date);
86 long timestamp = cal.getTimeInMillis(); 90 long timestamp = cal.getTimeInMillis();
87 - Calendar calendar = Calendar.getInstance();  
88 - calendar.add(Calendar.DATE,+1);  
89 - SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmss");  
90 - String motoday = sdf.format(calendar.getTime());  
91 - //Timestamp ts = new Timestamp(System.currentTimeMillis());  
92 - long currentTime=Calendar.getInstance().getTimeInMillis();  
93 - // System.out.println(ts+"---..ts..----");  
94 - // Date date = new Date();  
95 - // SimpleDateFormat sd = new SimpleDateFormat("MMddHHmmss");  
96 - // String ss = sd.format(date);  
97 - return String.valueOf(timestamp);  
98 - //return motoday; 91 + return String.valueOf(timestamp).substring(0, 10);
99 } 92 }
  93 +
100 /** 94 /**
101 - * 签名算法:signature 字符串格式,由三部分组成:GET+”\n”+Expire+”\n”+Resource 95 + * 签名算法:signature 字符串格式,由三部分组成:GET+”\n”+Expire+”\n”+Resource
102 * 2016 0602171212 96 * 2016 0602171212
  97 + *
103 * @return 98 * @return
104 - * @throws UnsupportedEncodingException 99 + * @throws UnsupportedEncodingException
105 */ 100 */
106 public static String signature(String streamName) throws UnsupportedEncodingException { 101 public static String signature(String streamName) throws UnsupportedEncodingException {
107 - 102 +
108 String method = "GET"; 103 String method = "GET";
109 - String Expire = String.valueOf(BaseUtil.getTimestamp()).substring(0, 10);  
110 - System.out.println(Expire+"---expire----");  
111 - String Resource = "app=live&method=add&nonce=cnlive&public=0&vdoid=12345&stream="+streamName;  
112 - Resource = java.net.URLEncoder.encode(Resource,"utf-8"); 104 + String Expire = BaseUtil.getTimestamp();
  105 + String nonce = getUUid();
  106 + System.out.println(Expire + "---expire----");
  107 + String Resource = "app=live&method=add&nonce=" + nonce + "&public=0&vdoid=12345&stream=" + streamName;
  108 + Resource = java.net.URLEncoder.encode(Resource, "utf-8");
113 String stringToSign = method + "\n" + Expire + "\n" + Resource; 109 String stringToSign = method + "\n" + Expire + "\n" + Resource;
114 // 2.计算 HMAC-SHA1 110 // 2.计算 HMAC-SHA1
115 - String signature = HMACSha1(stringToSign, AK_SECRET);  
116 - System.out.println(signature+"------");  
117 - signature =java.net.URLEncoder.encode(signature,"utf-8"); 111 + String signature = HMACSha1(stringToSign, SECRET_KEY);
  112 + System.out.println(signature + "---signature---");
  113 + signature = java.net.URLEncoder.encode(signature, "utf-8");
118 return signature; 114 return signature;
119 } 115 }
120 116
121 - public static String UUid() { 117 + public static String getUUid() {
122 String uuid = UUID.randomUUID().toString(); 118 String uuid = UUID.randomUUID().toString();
123 uuid = uuid.replaceAll("-", ""); 119 uuid = uuid.replaceAll("-", "");
124 return uuid; 120 return uuid;
@@ -150,7 +146,6 @@ public class BaseUtil { @@ -150,7 +146,6 @@ public class BaseUtil {
150 String result = ""; 146 String result = "";
151 try { 147 try {
152 URL realUrl = new URL(url); 148 URL realUrl = new URL(url);
153 -  
154 /* 149 /*
155 * http header 参数 150 * http header 参数
156 */ 151 */
@@ -286,14 +281,17 @@ public class BaseUtil { @@ -286,14 +281,17 @@ public class BaseUtil {
286 String hhhh = MD5Base64(hh); 281 String hhhh = MD5Base64(hh);
287 System.out.println(hh); 282 System.out.println(hh);
288 System.out.println(".............."); 283 System.out.println("..............");
289 - //String str = signature();  
290 - //System.out.println(str);//L2muBoImPzyvwyzfBtGXEu+wRNE=  
291 - /*// 发送GET请求  
292 - String ak_id1 = "fWUN8KDtJZJGo0ArQ4qo"; //用户ak  
293 - String ak_secret1 = "UIWPYVeaV8kq2vWImDkLBvWs/pwmTJVF9MCNHDsC"; // 用户ak_secret  
294 - String url1 = "http://cnlive.uplive.ks-cdn.com?signature=L2muBoImPzyvwyzfBtGXEu+wRNE=&accesskey=fWUN8KDtJZJGo0ArQ4qo&expire=1436976000&method=add&app=live&name=498122_57";  
295 - System.out.println("response body:" +sendGet(url1, ak_id1, ak_secret1));*/  
296 - //long d = getTimestamp();  
297 - //System.out.println(d); 284 + // String str = signature();
  285 + // System.out.println(str);//L2muBoImPzyvwyzfBtGXEu+wRNE=
  286 + /*
  287 + * // 发送GET请求 String ak_id1 = "fWUN8KDtJZJGo0ArQ4qo"; //用户ak String
  288 + * ak_secret1 = "UIWPYVeaV8kq2vWImDkLBvWs/pwmTJVF9MCNHDsC"; //
  289 + * 用户ak_secret String url1 =
  290 + * "http://cnlive.uplive.ks-cdn.com?signature=L2muBoImPzyvwyzfBtGXEu+wRNE=&accesskey=fWUN8KDtJZJGo0ArQ4qo&expire=1436976000&method=add&app=live&name=498122_57";
  291 + * System.out.println("response body:" +sendGet(url1, ak_id1,
  292 + * ak_secret1));
  293 + */
  294 + // long d = getTimestamp();
  295 + // System.out.println(d);
298 } 296 }
299 } 297 }