小节上传视频功能(后端)

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)再次测试

相关推荐
weixin_462428473 分钟前
使用 Caffeine 缓存并在业务方法上通过注解实现每3到5秒更新缓存
java·缓存
程序媛小果5 分钟前
基于java+SpringBoot+Vue的桂林旅游景点导游平台设计与实现
java·vue.js·spring boot
骑鱼过海的猫1237 分钟前
【java】java通过s3访问ceph报错
java·ceph·iphone
杨充13 分钟前
13.观察者模式设计思想
java·redis·观察者模式
Lizhihao_15 分钟前
JAVA-队列
java·开发语言
喵叔哟24 分钟前
重构代码之移动字段
java·数据库·重构
喵叔哟24 分钟前
重构代码之取消临时字段
java·前端·重构
fa_lsyk27 分钟前
maven环境搭建
java·maven
Daniel 大东1 小时前
idea 解决缓存损坏问题
java·缓存·intellij-idea
青锐CC1 小时前
webman使用中间件验证指定的控制器及方法[青锐CC]
中间件·前端框架·php