SpringBoot 实现视频分段播放(通过进度条来加载视频)

embedded/2024/9/23 9:25:26/

需求:现在我本地电脑中有一个文件夹,文件夹中都是视频,需要实现视频播放的功能。

问题:如果通过类似 SpringBoot static 文件夹的方式来实现,客户端要下载好完整的视频之后才可以播放,并且服务端也会占用大量宽带。所以这里考虑采取视频分段的方式进行下载,客户端播放视频时,可以通过进度条调节视频进度。

实现方法:在 SpringBoot 中支持自定义资源请求处理器,通过自定义的资源处理器来实现。

依赖:主要就是 SpringBoot 的相关依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>

编写一个自定义资源请求处理器,需要继承 ResourceHttpRequestHandler 重写 getResource() 方法。该自定义处理器还需要加上 @Component 注解。

在案例中 getResource() 方法中,需要拿到对应的视频地址,通过 Controller 层的代码通过 request.setAttribute("video-uri", videoUri) 设置 video-uri 的 value,value 可以是视频的本地路径,也可以是视频的唯一标识,需要按照自己的业务来传值。

import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;import javax.servlet.http.HttpServletRequest;/*** @author 17279*/
@Component
public class VideoResourceHttpRequestHandler extends ResourceHttpRequestHandler {// 只需要重写 getResource 方法@Overrideprotected Resource getResource(HttpServletRequest request) {// 这里的 videoUri 是在 Controller 中通过 request.setAttribute("video-uri", videoUri) 进来的值String videoUri = (String) request.getAttribute("video-uri"); // D:\Downloads\test.mp4// 可以通过 videoUri 传递的值根据我们的业务查找对应的文件return new FileSystemResource(videoUri);}
}

编写 Controller 层代码:

这里通过 request.setAttribute("video-uri", videoUri) 添加 video-uri 的 value,自定义处理中通过这个 value 找到向前端返回的视频。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;/*** @author 17279*/@RestController
@RequestMapping("video")
public class VideoController {@Autowiredprivate ResourceHttpRequestHandler resourceHttpRequestHandler;/*** @param fileName fileName 前端传递过来的视频文件名名称*/@GetMapping(value = "/{fileName}")public void video(@PathVariable("fileName") String fileName, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {// 视频文件根目录【换成实际的视频根目录】final String videoBasePath = "D:/Downloads/";// 视频根目录 + 文件名称,找到对应的文件String videoUri = videoBasePath + fileName;Path videoPath = Paths.get(videoUri);if (Files.exists(videoPath)) {// 如果找到视频,那么返回视频信息String contentType = Files.probeContentType(videoPath);if (contentType != null && !"".equals(contentType)) {response.setContentType(contentType);}// video-path 主要是将视频的地址传递给自定义的资源处理器处使用request.setAttribute("video-uri", videoUri);resourceHttpRequestHandler.handleRequest(request, response);} else {// 如果文件不存在,那么跳到 404 页面response.setStatus(HttpServletResponse.SC_NOT_FOUND);response.setCharacterEncoding(StandardCharsets.UTF_8.toString());}}
}

访问地址:http://localhost/video/test.mp4

访问效果:这里设置的视频跟目录是 D:/Downloads/,前端传递的 fileName 为 test.mp4,所以实际返回前端的文件为 D:/Downloads/test.mp4

在这里插入图片描述


http://www.ppmy.cn/embedded/56593.html

相关文章

scala基础

scala基础&#xff1a; hello world: 写scala可运行文件的注意事项1、如果一个scala文件要运行&#xff0c;class要改成object2、如果是class&#xff0c;就仅单纯代表一个类&#xff0c;如果是object代表的是单例对象3、scala语法中&#xff0c;一句话结束不需要加分号4、scal…

CEPH 系统盘挂了,如何使用数据盘恢复

硬盘损坏是早晚的时&#xff0c;CEHP数据盘坏了&#xff0c;使用CEPH的基本都轻车熟路了&#xff0c;如果系统盘坏了呢&#xff1f;不知道的可能会采取整个系统盘全做的方式 前提条件&#xff1a;使用cephadm搭建集群 如果换服务器&#xff0c;请确保CEPH数据盘放到其它服务器上…

【HICE】dns正向解析

1.编辑仓库 2.挂载 3.下载软件包 4.编辑named.conf 5.编辑named.haha 6.重启服务 7.验证本地域名是否解析

绿色金融相关数据合集(2007-2024年 具体看数据类型)

数据类型&#xff1a; 1.绿色债券数据&#xff1a;2014-2023 2.绿色信贷相关数据&#xff1a;2007-2022 3.全国各省及地级市绿色金融指数&#xff1a;1990-2022 4.碳排放权交易明细数据&#xff1a;2013-2024 5.绿色金融试点DID数据&#xff1a;2010-2023 数据来源&#…

Python应用开发——30天学习Streamlit Python包进行APP的构建(13)

st.chat_input 显示聊天输入窗口小部件。 Function signature[source]st.chat_input(placeholder="Your message", *, key=None, max_chars=None, disabled=False, on_submit=None, args=None, kwargs=None) Returns(str or None) The current (non-empty) value of…

抖音微短剧小程序入驻指南

一、抖音微短剧小程序类目和准入要求是什么&#xff1f; 可以明确的告诉你抖音微短剧小程序入驻是需要报白的&#xff0c;属于定邀类目&#xff0c;官方准入要求如下&#xff1a; 类目要求&#xff1a;文娱-微短剧 定向准入&#xff0c;填写“【微短剧】类目定向邀约申请表”…

Java跳出循环的四种方式

1、continue,break,return continue&#xff1a;跳出当前层循环的当前语句&#xff0c;执行当前层循环的下一条语句。   continue标签 break&#xff1a;跳出当前层循环。 break标签&#xff1a;多层循环时&#xff0c;跳到具体某层循环。 return&#xff1a;结束所有循环…

Selenium的这些自动化测试技巧你知道几个?

Selenium自动化测试技巧 与以前瀑布式开发模式不同&#xff0c;现在软件测试人员具有使用自动化工具执行测试用例套件的优势&#xff0c;而以前&#xff0c;测试人员习惯于通过测试脚本执行来完成测试。 但自动化测试的目的不是完全摆脱手动测试&#xff0c;而是最大程度地减少…