接口URL
http://api.polyv.net/live/v3/common/upload-image
接口说明
(接口调用有频率限制,详细请查看)
1、接口用于上传接口所需图片,同时获取图片地址
2、通用参数通过普通url参数传递(timestamp, appId, sign)
3、接口支持https
返回结果支持格式
JSON
请求方式
POST
请求数限制
TRUE
请求参数
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
appId | 是 | string | 从API设置中获取,在直播系统登记的appId |
timestamp | 是 | long | 13位当前时间戳 |
sign | 是 | string | 签名值 |
file | 是 | MultipartFile | 图片文件,支持同时上传不超过6个 |
type | 是 | string | 上传图片类型 |
#图片类型的值(文件支持图片类型为.jpg|.jpeg|.png,暖场图片另外支持.gif)
类型值 | 说明 |
---|---|
coverImage | 频道图标,建议140 x 140 大小的图标,支持jpg、jpeg、png格式,文件大小不超过 2M |
splashImage | 直播引导图,建议 750 x 1334 大小的图片,支持jpg、jpeg、png格式,大小不超过 2M |
logoImage | 播放器logo,建议不大于 140 x 50 大小的图片,支持jpg、jpeg、png格式,文件大小不超过 2M |
adminAvatar | 聊天室管理员头像,建议 140 x 140 大小的图标,文件大小不超过2M |
assistantAvatar | 助教头像,建议 140 x 140 大小的图标,文件大小不超过2M |
authCodeImage | 授权观看二维码图片, 最大不超过 200K |
warmImage | 暖场图片, 建议1280 x 720,图片大小不超过 2M,支持 jpg、jpeg、png、gif 格式 |
adImage | 广告栏图片,建议750 x 120,支持png、jpg等文件格式,最大不超过2 M |
startAdImage | 片头广告图片,建议 1280 x 720 大小的图片 ,文件大小不超过 4 M |
stopAdImage | 暂停广告图片,建议 1280 x 720 大小的图片 ,文件大小不超过 4 M |
goodImage | 打赏图标,建议 180 x 180 大小的图标,文件大小不超过 300 k |
invitationImage | 邀请卡图片,建议 750 x 1334 大小的图片,支持jpg、jpeg、png格式,大小不超过 4 M |
menuImage | 频道菜单图片, 最大不能超过为 2M |
注:这里的channelId,appId,timestamp,sign必须通过url传参,如:
http://api.polyv.net/live/v3/common/upload-image?appId={{appId}}×tamp={{timestamp}}&sign={{sign}}&type={{type}}
响应成功JSON示例:
{
"code": 200,
"status": "success",
"message": "",
"data": [
"//liveimages.videocc.net/uploaded/images/2019/10/fgxveewxwi.jpg"
]
}
响应失败JSON示例:
#参数错误
{
"code": 400,
"status": "error",
"message": "param validate error",
"data": 400
}
#未输入appId
{
"code": 400,
"status": "error",
"message": "appId is required.",
"data": ""
}
#appId不正确
{
"code": 400,
"status": "error",
"message": "application not found.",
"data": ""
}
#时间戳错误
{
"code": 400,
"status": "error",
"message": "invalid timestamp.",
"data": ""
}
#签名错误
{
"code": 403,
"status": "error",
"message": "invalid signature.",
"data": ""
}
#上传图片个数超过6个的限制
{
"code": 400,
"status": "error",
"message": "upload files over limit",
"data": ""
}
#上传图片大小超过限制,大小请参考上面的类型表
{
"code": 400,
"status": "error",
"message": "upload file size exceeds the limit",
"data": ""
}
#上传图片类型错误
{
"code": 400,
"status": "error",
"message": "upload file format error:.mp4",
"data": ""
}
#文件上传错误
{
"code": 400,
"status": "error",
"message": "undefined error",
"data": ""
}
字段说明
参数名 | 说明 |
---|---|
code | 状态码,成功为200,签名失败为403,参数错误为400,服务端错误为500 |
status | 成功为success,错误时为error |
message | 成功为"",错误时为错误描述信息 |
data | 成功时返回图片url列表 |
java请求示例
import com.live.util.EncryptionUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Demo {
private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(15000).setConnectTimeout(15000)
.setConnectionRequestTimeout(15000).build();
public static void main(String[] args) {
String url = "http://api.polyv.net/live/v3/common/upload-image";
String appId = "xxxxxxxxxx";
String key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
Map<String, String> map = new HashMap<>();
long timestamp = System.currentTimeMillis();
map.put("appId", appId);
map.put("timestamp", String.valueOf(timestamp));
map.put("type", "coverImage");
File file = new File("C:\Users\polyv\Desktop\timg.jpg");
List<File> files = new ArrayList<>();
files.add(file);
String sign = getSign(map, key);
map.put("sign", sign);
String content = sendHttpPost(url, map, files);
System.out.println(content);
}
/**
* 发送 post请求(带文件)
* @param httpUrl 地址
* @param maps 参数
* @param fileLists 附件
*/
public static String sendHttpPost(String httpUrl, Map<String, String> maps, List<File> fileLists) {
HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
MultipartEntityBuilder meBuilder = MultipartEntityBuilder.create();
for (String key : maps.keySet()) {
meBuilder.addPart(key, new StringBody(maps.get(key), ContentType.TEXT_PLAIN));
}
for (File file : fileLists) {
FileBody fileBody = new FileBody(file);
meBuilder.addPart("file", fileBody);
}
HttpEntity reqEntity = meBuilder.build();
httpPost.setEntity(reqEntity);
return sendHttpPost(httpPost);
}
/**
* 发送Post请求
* @param httpPost
* @return
*/
private static String sendHttpPost(HttpPost httpPost) {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity;
String responseContent = null;
try {
// 创建默认的httpClient实例.
httpClient = HttpClients.createDefault();
httpPost.setConfig(requestConfig);
// 执行请求
response = httpClient.execute(httpPost);
entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
// ...
} finally {
try {
// 关闭连接,释放资源
if (response != null) {
response.close();
}
if (null != httpPost) {
httpPost.releaseConnection();
}
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
// ...
}
}
return responseContent;
}
/**
* 根据map里的参数构建加密串
* @param map
* @param secretKey
* @return
*/
protected static String getSign(Map<String, String> map, String secretKey) {
Map<String, String> params = paraFilter(map);
// 处理参数,计算MD5哈希值
String concatedStr = concatParams(params);
String plain = secretKey + concatedStr + secretKey;
String encrypted = EncryptionUtils.md5Hex(plain);
// 32位大写MD5值
return encrypted.toUpperCase();
}
/**
* 对params根据key来排序并且以key1=value1&key2=value2的形式拼接起来
* @param params
* @return
*/
private static String concatParams(Map<String, String> params) {
List<String> keys = new ArrayList<>(params.keySet());
Collections.sort(keys);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
String value = params.get(key);
sb.append(key).append(value);
}
return sb.toString();
}
/**
* 除去数组中的空值和签名参数
* @param sArray 签名参数组
* @return 去掉空值与签名参数后的新签名参数组
*/
private static Map<String, String> paraFilter(Map<String, String> sArray) {
Map<String, String> result = new HashMap<>();
if (sArray == null || sArray.size() <= 0) {
return result;
}
for (String key : sArray.keySet()) {
String value = sArray.get(key);
if (value == null || value.equals("") || key.equalsIgnoreCase("sign")
|| key.equalsIgnoreCase("sign_type")) {
continue;
}
result.put(key, value);
}
return result;
}
}
php请求示例
<?php
//引用config.php
include 'config.php';
//接口需要的参数(非sign)赋值
$appId = "XXXXXXXX";
$type = "coverImage";
$imgfile = 'C:\Users\polyv\Desktop\timg.jpg';
$params = array(
'appId'=>$appId,
'timestamp'=>$timestamp,
'type'=>$type
);
//生成sign
$sign = getSign($params); //详细查看config.php文件的getSign方法
$data = array(
'appId' => $appId,
'timestamp' => $timestamp,
'sign' => $sign,
'type'=>$type,
'file' => new CURLFile(realpath($imgfile))
);
$url = "http://api.polyv.net/live/v3/common/upload-image";
$ch = curl_init() or die ( curl_error() );
curl_setopt( $ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 360);
$reponse = curl_exec ( $ch );
curl_close ( $ch );
print_r($reponse);
?>