springboot 视频分段加载在线播放

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

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

1.项目结构

2.pom依赖

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

3.启动类

java 复制代码
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);//初始化服务
    }
    @Override
    public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
    }
}
  1. ResourceHttpRequestHandler实现类
java 复制代码
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";

    @Override
    protected Resource getResource(HttpServletRequest request) {
        String filePath = (String) request.getAttribute(file_key);
        return new FileSystemResource(filePath);
    }
}
  1. 接口
java 复制代码
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 {

    @Autowired
    private 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);
    }
}

运行结果

相关推荐
四谎真好看8 分钟前
Java 学习笔记(进阶篇2)
java·笔记·学习
上官浩仁22 分钟前
springboot ioc 控制反转入门与实战
java·spring boot·spring
Access开发易登软件31 分钟前
Access开发导出PDF的N种姿势,你get了吗?
后端·低代码·pdf·excel·vba·access·access开发
叫我阿柒啊1 小时前
从Java全栈到前端框架:一位程序员的实战之路
java·spring boot·微服务·消息队列·vue3·前端开发·后端开发
mqiqe1 小时前
架构-亿级流量性能调优实践
java·架构
中国胖子风清扬1 小时前
Rust 序列化技术全解析:从基础到实战
开发语言·c++·spring boot·vscode·后端·中间件·rust
bobz9652 小时前
分析 docker.service 和 docker.socket 这两个服务各自的作用
后端
野犬寒鸦2 小时前
力扣hot100:旋转图像(48)(详细图解以及核心思路剖析)
java·数据结构·后端·算法·leetcode
七夜zippoe2 小时前
AI+Java 守护你的钱袋子!金融领域的智能风控与极速交易
java·人工智能·金融
岁忧2 小时前
(LeetCode 面试经典 150 题) 200. 岛屿数量(深度优先搜索dfs || 广度优先搜索bfs)
java·c++·leetcode·面试·go·深度优先