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

运行结果

相关推荐
QQ_43766431420 分钟前
C++11 右值引用 Lambda 表达式
java·开发语言·c++
永卿00121 分钟前
设计模式-迭代器模式
java·设计模式·迭代器模式
誰能久伴不乏29 分钟前
Linux如何执行系统调用及高效执行系统调用:深入浅出的解析
java·服务器·前端
itLaity40 分钟前
基于Kafka实现简单的延时队列
spring boot·分布式·kafka
慕y2741 小时前
Java学习第七十二部分——Zookeeper
java·学习·java-zookeeper
midsummer_woo1 小时前
基于spring boot的医院挂号就诊系统(源码+论文)
java·spring boot·后端
_Aaron___1 小时前
面向对象的三大特性---多态
java
Kiri霧1 小时前
IntelliJ IDEA
java·ide·kotlin·intellij-idea
daixin88482 小时前
什么是缓存雪崩?缓存击穿?缓存穿透?分别如何解决?什么是缓存预热?
java·开发语言·redis·缓存
Olrookie2 小时前
若依前后端分离版学习笔记(三)——表结构介绍
笔记·后端·mysql