当前位置: 首页 > ds >正文

springboot 视频分段加载在线播放

页面访问视频资源 前端播放加载部分视频,每次选中一个时间点后 往后加载一部分视频,主要用以节省网络传输的数据量

通过ResourceHttpRequestHandler类实现,ResourceHttpRequestHandler支持分片加载,前端请求头中携带Range: bytes = 0-10001,就可以从后台自动截取对应大小视频内容

1.项目结构

2.pom依赖

        <dependency><!--springboot启动依赖--><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>1.5.7.RELEASE</version></dependency>

3.启动类

package org.tmp;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
/*** 入口*/
@SpringBootApplication
public class Application implements EmbeddedServletContainerCustomizer {/*** 入口*/public static void main(String[] args) throws Exception {SpringApplication.run(Application.class, args);//初始化服务}@Overridepublic void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {}
}

4. ResourceHttpRequestHandler实现类

package org.tmp.test;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;/*** */
@Component
public class MyRequestHandler extends ResourceHttpRequestHandler {public final static String file_key = "my_file";@Overrideprotected Resource getResource(HttpServletRequest request) {String filePath = (String) request.getAttribute(file_key);return new FileSystemResource(filePath);}
}

5. 接口

package org.tmp.test;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;@Controller
@RequestMapping(value = "/api")
public class VideoApiController {@Autowiredprivate MyRequestHandler myRequestHandler;@RequestMapping(value = "/video")public void video(HttpServletRequest request, HttpServletResponse response) throws Exception {// 视频根目录 + 文件名称,找到对应的文件String path = "C:\\Users\\admin\\Desktop\\mp4\\1.mp4";//本地文件Path videoPath = Paths.get(path);if (!Files.exists(videoPath)) {response.setStatus(HttpServletResponse.SC_NOT_FOUND);response.setCharacterEncoding(StandardCharsets.UTF_8.toString());}String contentType = Files.probeContentType(videoPath);if (contentType != null && !contentType.isEmpty()) {//设置文件的响应类型response.setContentType(contentType);}request.setAttribute(MyRequestHandler.file_key, path);//将视频的地址传递给自定义的资源处理器myRequestHandler.handleRequest(request, response);System.out.println(contentType);}
}

运行结果

http://www.xdnf.cn/news/2865.html

相关文章:

  • 【含文档+PPT+源码】基于微信小程序的旅游论坛系统的设计与实现
  • Linux中的计划任务
  • Python----卷积神经网络(卷积为什么能识别图像)
  • 顶会idea:Mamba+CNN暴力涨点新突破!
  • vue复习91~135
  • Blender插件 幽灵拖尾特效动画 Ghosts V1.0
  • 重测序关系矩阵构建方式汇总
  • 什么是SCADA系统?
  • Redis ⑦-set | Zset
  • [OS] POSIX C库介绍
  • pgrep和pkill命令详解
  • Dockerfile讲解与示例汇总
  • C#进阶学习(十六)C#中的迭代器
  • VASP 教程:VASP 结合 phonopy 计算硅的声子谱
  • Electron 入门指南
  • [minilibc] 库文件的调用放置
  • 案例篇:如何用tcpdump和Wireshark识别潜在威胁
  • 大学之大:韩国科学技术研究院2025.4.28
  • Python依据卫星TLE轨道根数,计算可见时间窗口
  • Web 基础与Nginx访问统计
  • SECS-I vs HSMS-SS vs HSMS-GS 通信控制对比明细表
  • TypeScript 实用类型深度解析:Partial、Pick、Record 的妙用
  • SQL常用数据清洗语句
  • Python爬虫学习路径与实战指南 02
  • 苍穹外卖10
  • React学习
  • Spring系列四:AOP切面编程第四部分
  • 计网分层体系结构(包括OSI,IP,两者对比和相关概念)
  • 免费LUT网站
  • 花费7元训练自己的GPT 2模型