SpringBoot 集成网易云信 VOD API 实现视频信息查询

1. Maven 依赖

首先,确保在pom.xml中引入了必要的依赖项,特别是Spring Boot的Web Starter:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- 其他依赖 -->
</dependencies>

2. 实现Controller层

NeteaseVODController类中,我们创建了一个RESTful API的端点,用于接受客户端的请求并返回视频文件的信息:

import com.ruoyi.ruoyity.sy.netease.service.INeteaseVODService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 网易云信视频文件查询Controller
 */
@RestController
@RequestMapping("/sy")
public class NeteaseVODController {

    @Autowired
    private INeteaseVODService neteaseVODService;

    /**
     * 查询视频文件信息
     * 
     * @param vid 视频Id
     * @return 视频信息JSON字符串
     */
    @GetMapping("/queryVideo")
    public String queryVideo(@RequestParam("vid") String vid) {
        return neteaseVODService.queryVideo(vid);
    }
}

这里的/sy/queryVideo端点通过GET请求接受视频ID (vid) 参数,然后调用INeteaseVODService服务层来查询视频信息。

3. 服务层的实现

在服务层,我们实现了INeteaseVODService接口,并在NeteaseVODServiceImpl类中提供了具体的查询逻辑。这个类负责与网易云信的VOD API进行交互:

import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.security.MessageDigest;

@Service
public class NeteaseVODServiceImpl implements INeteaseVODService {

    private static final String API_URL = "https://vcloud.163.com/app/vod/video/get";
    private static final String APP_KEY = "你的AppKey";  // 替换为你的AppKey
    private static final String APP_SECRET = "你的AppSecret";  // 替换为你的AppSecret
    private static final String NONCE = "1";

    @Override
    public String queryVideo(String vid) {
        try {
            String curTime = String.valueOf(System.currentTimeMillis() / 1000L);
            String checkSum = getCheckSum(APP_SECRET, NONCE, curTime);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            headers.set("AppKey", APP_KEY);
            headers.set("Nonce", NONCE);
            headers.set("CurTime", curTime);
            headers.set("CheckSum", checkSum);

            String requestBody = "{\"vid\":" + vid + "}";
            HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);

            RestTemplate restTemplate = new RestTemplate();
            ResponseEntity<String> response = restTemplate.exchange(
                    API_URL,
                    HttpMethod.POST,
                    entity,
                    String.class
            );

            return response.getBody();

        } catch (Exception e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        }
    }

    private String getCheckSum(String appSecret, String nonce, String curTime) {
        try {
            String input = appSecret + nonce + curTime;
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
            byte[] hash = messageDigest.digest(input.getBytes("UTF-8"));
            StringBuilder hexString = new StringBuilder();

            for (byte b : hash) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }

            return hexString.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

 

4. 请求与响应的安全性

在与网易云信的API交互时,需要生成CheckSum以确保请求的安全性。这通过SHA-1算法来实现:

private String getCheckSum(String appSecret, String nonce, String curTime) {
    try {
        String input = appSecret + nonce + curTime;
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
        byte[] hash = messageDigest.digest(input.getBytes("UTF-8"));
        StringBuilder hexString = new StringBuilder();

        for (byte b : hash) {
            String hex = Integer.toHexString(0xff & b);
            if (hex.length() == 1) hexString.append('0');
            hexString.append(hex);
        }

        return hexString.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

6. 测试API

项目运行后,可以使用Postman或浏览器来测试API,例如:

GET http://localhost:8080/sy/queryVideo?vid=123456

结论

请访问: 一线网资源-全网一站式平台

如何使用Spring Boot来集成网易云信的视频点播API,并实现了查询视频信息的功能。
 

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yixian123.com

谢谢打赏,祝老板心想事成

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值