1、创建新模块service_vod
2、在service_vod的Pom文件中引入依赖
(1)确认父工程guli_parent引入依赖的版本
(2)在service_vod的Pom文件中引入依赖
java
<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.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>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
</dependencies>
3、创建配置文件application.properties
java
# 服务端口
server.port=8003
# 服务名
spring.application.name=service-vod
# 环境设置:dev、test、prod
spring.profiles.active=dev
4、在对应目录下,创建启动类
java
package com.atguigu.vodservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@ComponentScan(basePackages={"com.atguigu"})
//@MapperScan("com.atguigu")
public class VodApplication {
public static void main(String[] args) {
SpringApplication.run(VodApplication.class, args);
}
}
java
5、在com/atguigu/vodservice/utils包下创建工具类
java
public class AliyunVodSDKUtils {
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;
}
}
6、创建VideoAdminController,并添加上传视频的方法
java
@Api(description="视频管理")
@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin
public class VideoAdminController {
@ApiOperation(value = "上传视频")
@PostMapping("uploadVideo")
public R uploadVideo(MultipartFile file){
try {
//1上传文件名
String filename = file.getOriginalFilename();
//2阿里云显示的名称
String title = filename.substring(0,filename.lastIndexOf("."));
//3获取文件流
InputStream inputStream = file.getInputStream();
//4创建请求
UploadStreamRequest request = new UploadStreamRequest(
"LTAI3buexRAagkdy", "A6hpWJbF3Zz6wj3jxuBe40Mwryt1Zz",
title,filename,inputStream);
//5创建对象,提交请求,获得响应
UploadVideoImpl uploadVideo = new UploadVideoImpl();
UploadStreamResponse response = uploadVideo.uploadStream(request);
//6从响应中获取视频id
String videoId ="";
if(response.isSuccess()){
videoId =response.getVideoId();
}else{
videoId =response.getVideoId();
}
return R.ok().data("videoId",videoId);
} catch (IOException e) {
throw new GuliException(20001,"上传失败");
}
}
}
7、测试
(1)出现问题
java
Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http
(2)定位问题
上传文件大小有限制,tomcat有限制。
(3)解决问题
java
# 最大上传单个文件大小:默认1M
spring.servlet.multipart.max-file-size=1024MB
# 最大置总上传的数据大小 :默认10M
spring.servlet.multipart.max-request-size=1024MB
(4)再次测试