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

运行结果

相关推荐
fhgfyrsg11 分钟前
【无标题】
java
佩奇的技术笔记25 分钟前
Java学习手册:关系型数据库基础
java·数据库·学习
forestsea32 分钟前
Maven 实现多模块项目依赖管理
java·maven
种时光的人41 分钟前
【Java多线程】计时器Timer/ScheduledExecutorService的使用
java·开发语言
会游泳的石头1 小时前
在Java项目中实现本地语音识别与热点检测,并集成阿里云智能语音服务
java·阿里云·语音识别
计算机毕设指导61 小时前
基于Springboot旅游网站系统【附源码】
java·开发语言·spring boot·后端·mysql·spring·旅游
fanTuanye1 小时前
Java 中的 设计模式详解
java·设计模式·工厂模式
caihuayuan52 小时前
关于vue+iview中tabs嵌套及实际应用
java·大数据·spring boot·后端·课程设计
武昌库里写JAVA2 小时前
iView Admin的side menu改为top menu
java·vue.js·spring boot·课程设计·宠物管理
开开心心就好2 小时前
快速搜索与管理PDF文档的专业工具
java·运维·windows·pdf·自动化·excel·音视频