大家好,又见面了,我是你们的朋友全栈君。
=============================
(一)引入依赖
代码语言:javascript复制<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>service</artifactId>
<groupId>com.atguigu</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service_vod</artifactId>
<dependencies>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod</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>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
</dependencies>
</project>
这里需要注意的是 aliyun-java-sdk-vod 飘红问题,aliyun-java-sdk-vod 没有开源,所以不能直接在pom文件中直接引用依赖。
通常情况下:
1、下载aliyun-sdk-vod-upload.jar
2、将测试样例解压,进入lib目录下,在命令行执行下面的命令
mvn install:install-file -DgroupId=com.aliyun -DartifactId=aliyun-sdk-vod-upload -Dversion=1.4.12 -Dpackaging=jar -Dfile=aliyun-java-vod-upload-1.4.12.jar
在pom文件中引入该依赖即可
代码语言:javascript复制<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-sdk-vod-upload</artifactId>
<version>1.4.12</version>
</dependency>
但有的时候确实不好使,小编就是这样
我的解决办法(直接导入jar包!!!):
(二)Spring Boot 后端部分
1、配置文件application.properties
代码语言:javascript复制# 服务端口
server.port=8003
# 服务名
spring.application.name=service-vod
# 环境设置:dev、test、prod
spring.profiles.active=dev
#阿里云 vod
#不同的服务器,地址不同
aliyun.vod.file.keyid=LTAI4GJ2dfYZAmAusqM4qwti
aliyun.vod.file.keysecret=JwctWzF9sxACGAX7FjZ5wd8ewvjCPl
# 最大上传单个文件大小:默认1M
spring.servlet.multipart.max-file-size=1024MB
# 最大置总上传的数据大小 :默认10M
spring.servlet.multipart.max-request-size=1024MB
2、controller
代码语言:javascript复制package com.atguigu.vod.controller;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.exceptionhandler.GuliException;
import com.atguigu.vod.Utils.ConstantVodUtils;
import com.atguigu.vod.Utils.InitVodCilent;
import com.atguigu.vod.service.VodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
@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);
}
//根据视频id删除阿里云视频
@DeleteMapping("removeAlyVideo/{id}")
public R removeAlyVideo(@PathVariable String id) {
try {
//初始化对象
DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
//创建删除视频request对象
DeleteVideoRequest request = new DeleteVideoRequest();
//向request设置视频id
request.setVideoIds(id);
//调用初始化对象的方法实现删除
client.getAcsResponse(request);
return R.Ok();
}catch(Exception e) {
e.printStackTrace();
throw new GuliException(20001,"删除视频失败");
}
}
//删除多个阿里云视频的方法
//参数多个视频id List videoIdList
@DeleteMapping("delete-batch")
public R deleteBatch(@RequestParam("videoIdList") List<String> videoIdList) {
vodService.removeMoreAlyVideo(videoIdList);
return R.Ok();
}
}
3、service
代码语言:javascript复制package com.atguigu.vod.service;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
public interface VodService {
//上传视频到阿里云
String uploadVideoAly(MultipartFile file);
void removeMoreAlyVideo(List<String> videoIdList);
}
package com.atguigu.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.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.exceptionhandler.GuliException;
import com.atguigu.vod.Utils.ConstantVodUtils;
import com.atguigu.vod.Utils.InitVodCilent;
import com.atguigu.vod.service.VodService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@Service
public class VodServiceImpl implements VodService {
代码语言:javascript复制@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;
}
}
@Override
public void removeMoreAlyVideo(List videoIdList) {
try {
//初始化对象
DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
//创建删除视频request对象
DeleteVideoRequest request = new DeleteVideoRequest();
//videoIdList值转换成 1,2,3
String videoIds = StringUtils.join(videoIdList.toArray(), ",");
//向request设置视频id
request.setVideoIds(videoIds);
//调用初始化对象的方法实现删除
client.getAcsResponse(request);
}catch(Exception e) {
e.printStackTrace();
throw new GuliException(20001,"删除视频失败");
}
}
}
代码语言:javascript复制### 4、utils
package com.atguigu.vod.Utils;
import com.aliyun.oss.ClientException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
public class InitVodCilent {
代码语言:javascript复制public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
String regionId = "cn-shanghai"; // 点播服务接入区域
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
}
代码语言:javascript复制package com.atguigu.vod.Utils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ConstantVodUtils implements InitializingBean {
@Value("${aliyun.vod.file.keyid}")
private String keyid;
@Value("${aliyun.vod.file.keysecret}")
private String keysecret;
public static String ACCESS_KEY_SECRET;
public static String ACCESS_KEY_ID;
@Override
public void afterPropertiesSet() throws Exception {
ACCESS_KEY_ID = keyid;
ACCESS_KEY_SECRET = keysecret;
}
}
# 资料分享
这是我从某优质机构弄来的一些资料,内容我认为确实称得上优质二字,**如需领取,请点赞这篇文章,关注我然后[点击这里即可免费领取](https://gitee.com/vip204888/java-p7)**
**首先分享一份学习大纲,内容较多,涵盖了互联网行业所有的流行以及核心技术,以截图形式分享:**
(亿级流量性能调优实战 一线大厂分布式实战 架构师筑基必备技能 设计思想开源框架解读 性能直线提升架构技术 高效存储让项目性能起飞 分布式扩展到微服务架构.........实在是太多了)
![](https://img-blog.csdnimg.cn/img_convert/a792073bb5945eb2c2c176db172d1445.png)
**其次分享一些技术知识,以截图形式分享一部分:**
Tomcat架构解析:
![](https://img-blog.csdnimg.cn/img_convert/f7c6ab16b1f6a6277d9b97ffa88bce09.png)
算法训练 高分宝典:
![](https://img-blog.csdnimg.cn/img_convert/d4c72090e0c712e8e78700fd3d8f943e.png)
Spring Cloud Docker微服务实战:
![](https://img-blog.csdnimg.cn/img_convert/e5d9d5f2214e544316daf9f1a0aefcdf.png)
**最后分享一波面试资料:**
> 切莫死记硬背,小心面试官直接让你出门右拐
1000道互联网Java面试题:
![](https://img-blog.csdnimg.cn/img_convert/64b7e62ce4ae7272ca437ad30ea027f6.png)
Java高级架构面试知识整理:
![](https://img-blog.csdnimg.cn/img_convert/7b27a79ff2dc621cdc216048eaedf580.png)
,关注我然后[点击这里即可免费领取](https://gitee.com/vip204888/java-p7)**
**首先分享一份学习大纲,内容较多,涵盖了互联网行业所有的流行以及核心技术,以截图形式分享:**
(亿级流量性能调优实战 一线大厂分布式实战 架构师筑基必备技能 设计思想开源框架解读 性能直线提升架构技术 高效存储让项目性能起飞 分布式扩展到微服务架构.........实在是太多了)
[外链图片转存中...(img-zdQNY2gp-1628509537297)]
**其次分享一些技术知识,以截图形式分享一部分:**
Tomcat架构解析:
[外链图片转存中...(img-7bYRNM0l-1628509537299)]
算法训练 高分宝典:
[外链图片转存中...(img-z5LjEMDt-1628509537301)]
Spring Cloud Docker微服务实战:
[外链图片转存中...(img-xz8D56gG-1628509537303)]
**最后分享一波面试资料:**
> 切莫死记硬背,小心面试官直接让你出门右拐
1000道互联网Java面试题:
[外链图片转存中...(img-7m2VKefz-1628509537305)]
Java高级架构面试知识整理:
[外链图片转存中...(img-qs25fdQy-1628509537306)]
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/145340.html原文链接:https://javaforall.cn