Bootstrap

springboot实现阿里云视频点播技术----上传视频到阿里云视频点播存储起来

一、开通阿里云视频点播
在这里插入图片描述
根据api文档引入对应的的sdk的依赖,阿里云有对应的demo,可以下载根据里面的demo进行修改。

<dependency>
	  <groupId>com.aliyun</groupId>
	  <artifactId>aliyun-java-sdk-vod</artifactId>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-sdk-vod-upload</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
</dependency>
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
</dependency>
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
</dependency>

下面写一个上传本地视频的demo

public class TestVod {

    public static void main(String[] args) {
        String title = "What If I Want to Move";
        String fileName = "D:\\BaiduNetdiskDownload\\项目资料\\1-阿里云上传测试视频\\6 - What If I Want to Move Faster.mp4";
        UploadVideoRequest request = new UploadVideoRequest(String accessKeyId, String accessKeySecret, title, fileName);
        /* 可指定分片上传时每个分片的大小,默认为2M字节 */
        request.setPartSize(2 * 1024 * 1024L);
        /* 可指定分片上传时的并发线程数,默认为1,(注:该配置会占用服务器CPU资源,需根据服务器情况指定)*/
        request.setTaskNum(1);

        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadVideoResponse response = uploader.uploadVideo(request);
        System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
        if (response.isSuccess()) {
            System.out.print("VideoId=" + response.getVideoId() + "\n");
        } else {
            /* 如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因 */
            System.out.print("VideoId=" + response.getVideoId() + "\n");
            System.out.print("ErrorCode=" + response.getCode() + "\n");
            System.out.print("ErrorMessage=" + response.getMessage() + "\n");
        }
    }

}

这里的accessKeyI和accessKeySecret是阿里云后天提供的,自己配置好即可。
如果是使用前端上传的话,建议使用是流式上传接口
下面是六十上传接口主要核心代码

@Service
public class VodServiceImpl implements VodService {
    public String ACCESS_KEY_ID="sssss";
    public String ACCESS_KEY_SECRET="sssss";
    @Override
    public String uploadAliVideo(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        String title = fileName.substring(0, fileName.lastIndexOf("."));
        UploadStreamRequest request = null;
        try {
            request = new UploadStreamRequest(ACCESS_KEY_ID, ACCESS_KEY_SECRET, title, fileName, file.getInputStream());
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            if (response.isSuccess()) {
                return response.getVideoId();
            } else { //如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因
                return response.getVideoId();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

但是你在测试传大视频文件的时候会发现如下报错

org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.
	at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.handleParseFailure(StandardMultipartHttpServletRequest.java:122)
	at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:115)
	at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.<init>(StandardMultipartHttpServletRequest.java:88)
	at org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:87)

原因是tomcat限制了的单个文件大小为1M,所以需要在application.properties里面修改如下配置

#最大上传文件大小,默认是1M
spring.servlet.multipart.max-file-size=1024MB
#最大量总上传文件的数据大小,默认10M
spring.servlet.multipart.max-request-size=1024MB

还有一个地方会限制文件上传大小。当前端调用的接口是通过nginx转发到服务端的时候,nginx也会有默认的文件上传大小限制,可以修改nginx配置文件来解决。
在nginx.conf中添加一句:client_max_body_size 1024m;
在这里插入图片描述

;