Maven: -腾讯云直播
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<!-- go to https://search.maven.org/search?q=tencentcloud-sdk-java and get the latest version. -->
<!-- 请到https://search.maven.org/search?q=tencentcloud-sdk-java查询所有版本,最新版本如下 -->
<version>3.1.324</version>
</dependency>
import lombok.extern.slf4j.Slf4j;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
/**
* @description TODO 腾讯云直播工具类
* @Author Administrator
* @Date 2021/7/16
* @Version 1.0
**/
@Slf4j
public class TencentYunVedioUtil {
private static final char[] DIGITS_LOWER =
{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/**
* 获取推流地址中鉴权信息
**/
public static String getPlugFlowAAA(String key, String streamName){
return getSafeUrl(key, streamName, getTxTime());
}
/**
* 获取过期时间(注意: 腾讯云直播过期时间的时间戳长度必须是10位)
**/
private static long getTxTime(){
/** #################################### 有效期向后推1天 ######################################### */
return new Date((System.currentTimeMillis() + (1000 * 60 * 60 * 24)) / 1000).getTime();
}
/*
* 鉴权信息 = KEY(鉴权key)+ streamName(根据业务获取) + txTime(推流地址过期时间)
*/
public static String getSafeUrl(String key, String streamName, long txTime) {
String input = new StringBuilder().
append(key).
append(streamName).
append(Long.toHexString(txTime).toUpperCase()).toString();
String txSecret = null;
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
txSecret = byteArrayToHexString(
messageDigest.digest(input.getBytes("UTF-8")));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return txSecret == null ? "" :
new StringBuilder().
append("txSecret=").
append(txSecret).
append("&").
append("txTime=").
append(Long.toHexString(txTime).toUpperCase()).
toString();
}
private static String byteArrayToHexString(byte[] data) {
char[] out = new char[data.length << 1];
for (int i = 0, j = 0; i < data.length; i++) {
out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4];
out[j++] = DIGITS_LOWER[0x0F & data[i]];
}
return new String(out);
}
/**
* @Desc //TODO UTC格式转化为ISO 8601 直播封禁时间格式化
* @Date 11:27 2021/8/3
* @Param [date] 解禁时间点
* @return java.lang.String
**/
public static String UtcConvertToIso8601(Date date){
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(tz); //获取时区
String nowAsISO = df.format(date);
System.out.println("本地时间Date--->UTC时间String:" + nowAsISO);
return nowAsISO;
}
}