Commit b1a49d9df047b996b348b19c73f7b2f3cdb8bc50
1 parent
8da282ed
util
Showing
1 changed file
with
372 additions
and
0 deletions
src/main/java/com/cnlive/mz/live/rest/BaseUtil.java.bak
0 → 100644
| 1 | +package com.cnlive.mz.live.rest; | |
| 2 | + | |
| 3 | +import java.io.BufferedReader; | |
| 4 | +import java.io.IOException; | |
| 5 | +import java.io.InputStreamReader; | |
| 6 | +import java.io.PrintWriter; | |
| 7 | +import java.io.UnsupportedEncodingException; | |
| 8 | +import java.net.URL; | |
| 9 | +import java.net.URLConnection; | |
| 10 | +import java.security.MessageDigest; | |
| 11 | +import java.text.DateFormat; | |
| 12 | +import java.text.ParseException; | |
| 13 | +import java.text.SimpleDateFormat; | |
| 14 | +import java.util.Calendar; | |
| 15 | +import java.util.Date; | |
| 16 | +import java.util.Locale; | |
| 17 | +import java.util.UUID; | |
| 18 | + | |
| 19 | +import javax.crypto.Mac; | |
| 20 | +import javax.crypto.spec.SecretKeySpec; | |
| 21 | + | |
| 22 | +import org.apache.commons.codec.binary.Base64; | |
| 23 | + | |
| 24 | +import com.cnlive.mz.commons.net.HttpReq; | |
| 25 | + | |
| 26 | +import sun.misc.BASE64Encoder; | |
| 27 | + | |
| 28 | +public class BaseUtil { | |
| 29 | + | |
| 30 | + private static final String ENCODING = "UTF-8"; | |
| 31 | +<<<<<<< HEAD | |
| 32 | + // AccessKey、SecretKey | |
| 33 | + private static final String SECRET_KEY = "iqljE4WxQaSMeSd1QP+PPYSpvtK0uWyH0/r6tgi7"; | |
| 34 | + private static final String ACCESS_KEY = "I3TC8a4xboN9kSJnUIQh"; | |
| 35 | +======= | |
| 36 | + private static final String AK_SECRET = "7SxVU5lFpqCEQIqxLprQat9BWoyZNKlKfSScTuIk"; | |
| 37 | + private static final String AK_ID = "I3TC8a4xboPUMe5xlp22"; | |
| 38 | +>>>>>>> 180640a4e451ae2b82ae745eb6b1d1651dc5f17f | |
| 39 | + | |
| 40 | + /* | |
| 41 | + * 计算MD5+BASE64 | |
| 42 | + */ | |
| 43 | + public static String MD5Base64(String s) { | |
| 44 | + if (s == null) | |
| 45 | + return null; | |
| 46 | + String encodeStr = ""; | |
| 47 | + byte[] utfBytes = s.getBytes(); | |
| 48 | + MessageDigest mdTemp; | |
| 49 | + try { | |
| 50 | + mdTemp = MessageDigest.getInstance("MD5"); | |
| 51 | + mdTemp.update(utfBytes); | |
| 52 | + byte[] md5Bytes = mdTemp.digest(); | |
| 53 | + BASE64Encoder b64Encoder = new BASE64Encoder(); | |
| 54 | + encodeStr = b64Encoder.encode(md5Bytes); | |
| 55 | + } catch (Exception e) { | |
| 56 | + throw new Error("Failed to generate MD5 : " + e.getMessage()); | |
| 57 | + } | |
| 58 | + return encodeStr; | |
| 59 | + } | |
| 60 | + | |
| 61 | + /* | |
| 62 | + * 哈希计算 HMAC-SHA1 base64 | |
| 63 | + */ | |
| 64 | + public static String HMACSha1(String data, String key) { | |
| 65 | + String result; | |
| 66 | + try { | |
| 67 | + SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1"); | |
| 68 | + Mac mac = Mac.getInstance("HmacSHA1"); | |
| 69 | + mac.init(signingKey); | |
| 70 | + byte[] rawHmac = mac.doFinal(data.getBytes()); | |
| 71 | + //result = new String(Base64.encodeBase64(rawHmac, false), ENCODING); | |
| 72 | + //result = (new BASE64Encoder()).encode(rawHmac); | |
| 73 | + result = new String(Base64.encodeBase64(rawHmac),ENCODING); | |
| 74 | + } catch (Exception e) { | |
| 75 | + throw new Error("Failed to generate HMAC : " + e.getMessage()); | |
| 76 | + } | |
| 77 | + return result; | |
| 78 | + } | |
| 79 | + | |
| 80 | + /** | |
| 81 | + * 获取当前时间戳 | |
| 82 | + * @return | |
| 83 | + */ | |
| 84 | + public static String getTimestamp(){ | |
| 85 | + DateFormat df = new SimpleDateFormat("yyyyMMdd"); | |
| 86 | + Date date = null; | |
| 87 | + try { | |
| 88 | + date = df.parse("20160607"); | |
| 89 | + } catch (ParseException e) { | |
| 90 | + e.printStackTrace(); | |
| 91 | + } | |
| 92 | + Calendar cal = Calendar.getInstance(); | |
| 93 | + cal.setTime(date); | |
| 94 | + long timestamp = cal.getTimeInMillis(); | |
| 95 | +<<<<<<< HEAD | |
| 96 | + System.out.println("------timestamp-----"+timestamp); | |
| 97 | + //return String.valueOf(timestamp).substring(0, 10); | |
| 98 | + return "1483200000"; | |
| 99 | +======= | |
| 100 | + Calendar calendar = Calendar.getInstance(); | |
| 101 | + calendar.add(Calendar.DATE,+1); | |
| 102 | + SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmss"); | |
| 103 | + String motoday = sdf.format(calendar.getTime()); | |
| 104 | + //Timestamp ts = new Timestamp(System.currentTimeMillis()); | |
| 105 | + long currentTime=Calendar.getInstance().getTimeInMillis(); | |
| 106 | + // System.out.println(ts+"---..ts..----"); | |
| 107 | + // Date date = new Date(); | |
| 108 | + // SimpleDateFormat sd = new SimpleDateFormat("MMddHHmmss"); | |
| 109 | + // String ss = sd.format(date); | |
| 110 | + return String.valueOf(timestamp); | |
| 111 | + //return motoday; | |
| 112 | +>>>>>>> 180640a4e451ae2b82ae745eb6b1d1651dc5f17f | |
| 113 | + } | |
| 114 | + /** | |
| 115 | + * 签名算法:signature 字符串格式,由三部分组成:GET+”\n”+Expire+”\n”+Resource | |
| 116 | + * 2016 0602171212 | |
| 117 | + * @return | |
| 118 | + * @throws UnsupportedEncodingException | |
| 119 | + */ | |
| 120 | + public static String signature(String streamName) throws UnsupportedEncodingException { | |
| 121 | + | |
| 122 | + String method = "GET"; | |
| 123 | +<<<<<<< HEAD | |
| 124 | + String expire = BaseUtil.getTimestamp(); | |
| 125 | + String app = "live"; | |
| 126 | + String methodadd = "add"; | |
| 127 | + String name = streamName; | |
| 128 | + app = java.net.URLEncoder.encode(app, "utf-8"); | |
| 129 | + methodadd = java.net.URLEncoder.encode(methodadd, "utf-8"); | |
| 130 | + name = java.net.URLEncoder.encode(name, "utf-8"); | |
| 131 | + System.out.println(expire + "---expire----"); | |
| 132 | + String Resource = "app="+app+"&method="+methodadd+"&name=" + name; | |
| 133 | + //Resource = java.net.URLEncoder.encode(Resource, "utf-8"); | |
| 134 | + String stringToSign = method + "\n" + expire + "\n" + Resource; | |
| 135 | + System.out.println("--stringToSign--"+stringToSign); | |
| 136 | + // 2.计算 HMAC-SHA1 | |
| 137 | + String signature = HMACSha1(stringToSign, SECRET_KEY); | |
| 138 | + System.out.println( "---signature---"+signature ); | |
| 139 | + signature = java.net.URLEncoder.encode(signature, "utf-8"); | |
| 140 | +======= | |
| 141 | + String Expire = String.valueOf(BaseUtil.getTimestamp()).substring(0, 10); | |
| 142 | + System.out.println(Expire+"---expire----"); | |
| 143 | + String Resource = "app=live&method=add&nonce=cnlive&public=0&vdoid=12345&stream="+streamName; | |
| 144 | + Resource = java.net.URLEncoder.encode(Resource,"utf-8"); | |
| 145 | + String stringToSign = method + "\n" + Expire + "\n" + Resource; | |
| 146 | + // 2.计算 HMAC-SHA1 | |
| 147 | + String signature = HMACSha1(stringToSign, AK_SECRET); | |
| 148 | + System.out.println(signature+"------"); | |
| 149 | + signature =java.net.URLEncoder.encode(signature,"utf-8"); | |
| 150 | +>>>>>>> 180640a4e451ae2b82ae745eb6b1d1651dc5f17f | |
| 151 | + return signature; | |
| 152 | + } | |
| 153 | + public static String signature(String methodNname,String streamName) throws UnsupportedEncodingException { | |
| 154 | + | |
| 155 | +<<<<<<< HEAD | |
| 156 | + String method = "GET"; | |
| 157 | + String expire = BaseUtil.getTimestamp(); | |
| 158 | + String app = "live"; | |
| 159 | + //String methodadd = methodNname; | |
| 160 | + //String name = streamName; | |
| 161 | + app = java.net.URLEncoder.encode(app, "utf-8"); | |
| 162 | + methodNname = java.net.URLEncoder.encode(methodNname, "utf-8"); | |
| 163 | + streamName = java.net.URLEncoder.encode(streamName, "utf-8"); | |
| 164 | + System.out.println(expire + "---expire----"); | |
| 165 | + String Resource = "app="+app+"&method="+methodNname+"&name=" + streamName; | |
| 166 | + //Resource = java.net.URLEncoder.encode(Resource, "utf-8"); | |
| 167 | + String stringToSign = method + "\n" + expire + "\n" + Resource; | |
| 168 | + System.out.println("--stringToSign--"+stringToSign); | |
| 169 | + // 2.计算 HMAC-SHA1 | |
| 170 | + String signature = HMACSha1(stringToSign, SECRET_KEY); | |
| 171 | + System.out.println( "---signature---"+signature ); | |
| 172 | + signature = java.net.URLEncoder.encode(signature, "utf-8"); | |
| 173 | + return signature; | |
| 174 | + } | |
| 175 | + | |
| 176 | + public static String getUUid() { | |
| 177 | +======= | |
| 178 | + public static String UUid() { | |
| 179 | +>>>>>>> 180640a4e451ae2b82ae745eb6b1d1651dc5f17f | |
| 180 | + String uuid = UUID.randomUUID().toString(); | |
| 181 | + uuid = uuid.replaceAll("-", ""); | |
| 182 | + return uuid; | |
| 183 | + } | |
| 184 | + public static String getAccessKey() { | |
| 185 | + return ACCESS_KEY; | |
| 186 | + } | |
| 187 | + | |
| 188 | + public static int random_7(int number) { | |
| 189 | + int x = (int) (Math.random() * 10); | |
| 190 | + for (int i = 0; i < number; i++) { | |
| 191 | + String num = Math.random() * 10 + ""; | |
| 192 | + } | |
| 193 | + return x; | |
| 194 | + } | |
| 195 | + | |
| 196 | + /* | |
| 197 | + * 等同于javaScript中的 new Date().toUTCString(); | |
| 198 | + */ | |
| 199 | + public static String toGMTString(Date date) { | |
| 200 | + SimpleDateFormat df = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.UK); | |
| 201 | + df.setTimeZone(new java.util.SimpleTimeZone(0, "GMT")); | |
| 202 | + return df.format(date); | |
| 203 | + } | |
| 204 | + | |
| 205 | + /* | |
| 206 | + * 发送POST请求 | |
| 207 | + */ | |
| 208 | + public static String sendPost(String url, String body, String ak_id, String ak_secret) { | |
| 209 | + PrintWriter out = null; | |
| 210 | + BufferedReader in = null; | |
| 211 | + String result = ""; | |
| 212 | + try { | |
| 213 | + URL realUrl = new URL(url); | |
| 214 | + | |
| 215 | + /* | |
| 216 | + * http header 参数 | |
| 217 | + */ | |
| 218 | + String method = "POST"; | |
| 219 | + String accept = "json"; | |
| 220 | + String content_type = "application/json"; | |
| 221 | + String path = realUrl.getFile(); | |
| 222 | + String date = toGMTString(new Date()); | |
| 223 | + | |
| 224 | + // 1.对body做MD5+BASE64加密 | |
| 225 | + String bodyMd5 = MD5Base64(body); | |
| 226 | + String stringToSign = method + "\n" + accept + "\n" + bodyMd5 + "\n" + content_type + "\n" + date + "\n" | |
| 227 | + + path; | |
| 228 | + // 2.计算 HMAC-SHA1 | |
| 229 | + String signature = HMACSha1(stringToSign, ak_secret); | |
| 230 | + // 3.得到 authorization header | |
| 231 | + String authHeader = "Dataplus " + ak_id + ":" + signature; | |
| 232 | + | |
| 233 | + // 打开和URL之间的连接 | |
| 234 | + URLConnection conn = realUrl.openConnection(); | |
| 235 | + // 设置通用的请求属性 | |
| 236 | + conn.setRequestProperty("accept", accept); | |
| 237 | + conn.setRequestProperty("content-type", content_type); | |
| 238 | + conn.setRequestProperty("date", date); | |
| 239 | + conn.setRequestProperty("Authorization", authHeader); | |
| 240 | + // 发送POST请求必须设置如下两行 | |
| 241 | + conn.setDoOutput(true); | |
| 242 | + conn.setDoInput(true); | |
| 243 | + // 获取URLConnection对象对应的输出流 | |
| 244 | + out = new PrintWriter(conn.getOutputStream()); | |
| 245 | + // 发送请求参数 | |
| 246 | + out.print(body); | |
| 247 | + // flush输出流的缓冲 | |
| 248 | + out.flush(); | |
| 249 | + // 定义BufferedReader输入流来读取URL的响应 | |
| 250 | + in = new BufferedReader(new InputStreamReader(conn.getInputStream())); | |
| 251 | + String line; | |
| 252 | + while ((line = in.readLine()) != null) { | |
| 253 | + result += line; | |
| 254 | + } | |
| 255 | + } catch (Exception e) { | |
| 256 | + System.out.println("发送 POST 请求出现异常!" + e); | |
| 257 | + e.printStackTrace(); | |
| 258 | + } | |
| 259 | + // 使用finally块来关闭输出流、输入流 | |
| 260 | + finally { | |
| 261 | + try { | |
| 262 | + if (out != null) { | |
| 263 | + out.close(); | |
| 264 | + } | |
| 265 | + if (in != null) { | |
| 266 | + in.close(); | |
| 267 | + } | |
| 268 | + } catch (IOException ex) { | |
| 269 | + ex.printStackTrace(); | |
| 270 | + } | |
| 271 | + } | |
| 272 | + return result; | |
| 273 | + } | |
| 274 | + | |
| 275 | + /* | |
| 276 | + * GET请求 | |
| 277 | + */ | |
| 278 | + | |
| 279 | + public static String sendGet(String url, String ak_id, String ak_secret) { | |
| 280 | + String result = ""; | |
| 281 | + BufferedReader in = null; | |
| 282 | + try { | |
| 283 | + URL realUrl = new URL(url); | |
| 284 | + /* | |
| 285 | + * http header 参数 | |
| 286 | + */ | |
| 287 | +// String method = "GET"; | |
| 288 | +// String Expire = "1436976000"; | |
| 289 | +// String Resource = "nonce=4e1f2519c626cbfbab1520c255830c26&public=0&vdoid=12345"; | |
| 290 | +// String content_type = "application/json"; | |
| 291 | +// String path = realUrl.getFile(); | |
| 292 | +// String date = toGMTString(new Date()); | |
| 293 | +// // 1.对body做MD5+BASE64加密 | |
| 294 | +// // String bodyMd5 = MD5Base64(body); | |
| 295 | +// String stringToSign = method + "\n" + Expire + "\n" + Resource; | |
| 296 | +// // 2.计算 HMAC-SHA1 | |
| 297 | +// String signature = HMACSha1(stringToSign, ak_secret); | |
| 298 | +// // 3.得到 authorization header | |
| 299 | +// String authHeader = "Dataplus " + ak_id + ":" + signature; | |
| 300 | + // 打开和URL之间的连接 | |
| 301 | + URLConnection connection = realUrl.openConnection(); | |
| 302 | + // 设置通用的请求属性 | |
| 303 | +// connection.setRequestProperty("accept", "json"); | |
| 304 | +// connection.setRequestProperty("content-type", content_type); | |
| 305 | +// connection.setRequestProperty("date", date); | |
| 306 | +// connection.setRequestProperty("Authorization", authHeader); | |
| 307 | + // 建立实际的连接 | |
| 308 | + connection.connect(); | |
| 309 | + // 定义 BufferedReader输入流来读取URL的响应 | |
| 310 | + in = new BufferedReader(new InputStreamReader(connection.getInputStream())); | |
| 311 | + String line; | |
| 312 | + while ((line = in.readLine()) != null) { | |
| 313 | + result += line; | |
| 314 | + } | |
| 315 | + } catch (Exception e) { | |
| 316 | + System.out.println("发送GET请求出现异常!" + e); | |
| 317 | + e.printStackTrace(); | |
| 318 | + } | |
| 319 | + // 使用finally块来关闭输入流 | |
| 320 | + finally { | |
| 321 | + try { | |
| 322 | + if (in != null) { | |
| 323 | + in.close(); | |
| 324 | + } | |
| 325 | + } catch (Exception e) { | |
| 326 | + e.printStackTrace(); | |
| 327 | + } | |
| 328 | + } | |
| 329 | + return result; | |
| 330 | + } | |
| 331 | + | |
| 332 | + public static void main(String[] args) throws Exception { | |
| 333 | + // 发送POST请求示例 | |
| 334 | + /* | |
| 335 | + * String ak_id = "fWUN8KDtJZJGo0ArQ4qo"; // 用户ak String ak_secret = | |
| 336 | + * "UIWPYVeaV8kq2vWImDkLBvWs/pwmTJVF9MCNHDsC"; // 用户ak_secret String url | |
| 337 | + * = "https://kmr-cn-beijing.ksyun.com"; String body = | |
| 338 | + * "{\"param1\": \"GET\n1436976000\nnonce=4e1f2519c626cbfbab1520c255830c26&public=0&vdoid=12345\", \"param2\":\"fWUN8KDtJZJGo0ArQ4qo\"}" | |
| 339 | + * ; System.out.println("response body:" + sendPost(url, body, AK_ID, | |
| 340 | + * AK_SECRET)); | |
| 341 | + */ | |
| 342 | + | |
| 343 | + Date date = new Date(); | |
| 344 | + System.out.println(toGMTString(date)); | |
| 345 | + String hh = HMACSha1("GET\n1436976124\nnonce=4e1f2519c626cbfbab1520c255830c26&public=1&vdoid=1", | |
| 346 | + "fWUN8KDtJZJGo0ArQ4qo"); | |
| 347 | + String hhhh = MD5Base64(hh); | |
| 348 | + System.out.println(hh); | |
| 349 | + System.out.println(".............."); | |
| 350 | +<<<<<<< HEAD | |
| 351 | + // String str = signature(); | |
| 352 | + // System.out.println(str);//L2muBoImPzyvwyzfBtGXEu+wRNE= | |
| 353 | + | |
| 354 | + // 发送GET请求 | |
| 355 | + String url1 = "http://cnlive.dashboard.ks-cdn.com/blacklist?signature=iDPKaesXXW3A26CScNyjpNkzxZs%3D&accesskey=I3TC8a4xboPUMe5xlp22&expire=1483200000&app=live&method=add&stream=310166_863"; | |
| 356 | + System.out.println("response body:" +sendGet(url1, ACCESS_KEY, SECRET_KEY)); | |
| 357 | + | |
| 358 | + // long d = getTimestamp(); | |
| 359 | + // System.out.println(d); | |
| 360 | +======= | |
| 361 | + //String str = signature(); | |
| 362 | + //System.out.println(str);//L2muBoImPzyvwyzfBtGXEu+wRNE= | |
| 363 | + /*// 发送GET请求 | |
| 364 | + String ak_id1 = "fWUN8KDtJZJGo0ArQ4qo"; //用户ak | |
| 365 | + String ak_secret1 = "UIWPYVeaV8kq2vWImDkLBvWs/pwmTJVF9MCNHDsC"; // 用户ak_secret | |
| 366 | + String url1 = "http://cnlive.uplive.ks-cdn.com?signature=L2muBoImPzyvwyzfBtGXEu+wRNE=&accesskey=fWUN8KDtJZJGo0ArQ4qo&expire=1436976000&method=add&app=live&name=498122_57"; | |
| 367 | + System.out.println("response body:" +sendGet(url1, ak_id1, ak_secret1));*/ | |
| 368 | + //long d = getTimestamp(); | |
| 369 | + //System.out.println(d); | |
| 370 | +>>>>>>> 180640a4e451ae2b82ae745eb6b1d1651dc5f17f | |
| 371 | + } | |
| 372 | +} | ... | ... |