-
@Description :
-
@Author :lenovo
-
@Date :2021/3/23 20:06
*/
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@ComponentScan(basePackages = {“com.nonglin”})
public class VodApplication {
public static void main(String[] args) {
SpringApplication.run(VodApplication.class,args);
}
}
[](()4、创建controller,service实现上传小节功能
=============================================================================================
[](()4.1、controller代码
package com.nonglin.vod.controller;
import com.nonglin.commonutils.R;
import com.nonglin.vod.service.VodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
/**
-
@Description :
-
@Author :lenovo
-
@Date :2021/3/23 20:13
*/
@RestController
@RequestMapping(“/eduvod/video”)
@CrossOrigin
public class VodController {
@Autowired
private VodService vodService;
//上传视频到阿里云
@PostMapping(“uploadAlyiVideo”)
public R uploadAlyiVideo(MultipartFile file) {
//返回上传视频id
String videoId = vodService.uploadVideoAly(file);
return R.ok().data(“videoId”,videoId);
}
}
[](()4.2、service代码
接口
package com.nonglin.vod.service;
import org.springframework.web.multipart.MultipartFile;
/**
-
@Description :
-
@Author :lenovo
-
@Date :2021/3/23 20:17
*/
public interface VodService {
String uploadVideoAly(MultipartFile file);
}
service实现类:
package com.nonglin.vod.service.impl;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.nonglin.vod.service.VodService;
import com.nonglin.vod.utils.ConstantVodUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
/**
-
@Description :
-
@Author :lenovo
-
@Date :2021/3/23 20:18
*/
@Service
public class VodServiceImpl implements VodService {
@Override
public String uploadVideoAly(MultipartFile file) {
try {
//accessKeyId, accessKeySecret
//fileName:上传文件原始名称
// 01.03.09.mp4
String fileName = file.getOriginalFilename();
//title:上传之后显示名称
String title = fileName.substring(0, fileName.lastIndexOf(“.”));
//inputStream:上传文件输入流
InputStream inputStream = file.getInputStream();
UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID,ConstantVodUtils.ACCESS_KEY_SECRET, title, fileName, inputStream);
UploadVideoImpl uploader = new UploadVideoImpl();
UploadStreamResponse response = uploader.uploadStream(request);
String videoId = null;
if (response.isSuccess()) {
videoId = response.getVideoId();
} else { //如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因
videoId = response.getVideoId();
}
return videoId;
}catch(Exception e) {
e.printStackTrace();
return null;
}
}
}
[](()5、工具类
====================================================================
package com.nonglin.vod.utils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import or 《大厂前端面试题解析+Web核心总结学习笔记+企业项目实战源码+最新高清讲解视频》无偿开源 徽信搜索公众号【编程进阶路】 g.springframework.stereotype.Component;
/**
-
@Description :
-
@Author :lenovo
-
@Date :2021/3/23 20:28
*/
@Component
public class ConstantVodUtils implements InitializingBean {
@Value(“${aliyun.vod.file.keyid}”)
private String keyid;
@Value(“${aliyun.vod.file.keysecret}”)
private String keysecret;