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);
    }
}

运行结果

相关推荐
kfyty7257 分钟前
轻量级 ioc 框架 loveqq,支持接口上传 jar 格式的 starter 启动器并支持热加载其中的 bean
java·jvm·ioc·jar·热加载
G探险者31 分钟前
为什么 Zookeeper 越扩越慢,而 Nacos 却越扩越快?
分布式·后端
早起鸟儿33 分钟前
docker-Dockerfile 配置
java·linux·运维·docker
云边小网安37 分钟前
java集合篇(六) ---- ListIterator 接口
java·开发语言·青少年编程·java集合
都叫我大帅哥43 分钟前
Spring WebFlux:响应式编程的“未来战士”还是“花架子”?
java·spring·flux
都叫我大帅哥1 小时前
Reactor 深度解析:响应式编程的「核反应堆」是如何工作的?
java·spring
不太厉害的程序员1 小时前
NC65配置xml找不到Bean
xml·java·后端·eclipse
不被定义的程序猿1 小时前
Golang 在 Linux 平台上的并发控制
开发语言·后端·golang
AntBlack1 小时前
Python : AI 太牛了 ,撸了两个 Markdown 阅读器 ,谈谈使用感受
前端·人工智能·后端
我在北国不背锅2 小时前
基于Java开发的浏览器自动化Playwright-MCP服务器
java·playwright·mcp