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

运行结果

相关推荐
东风破_2 分钟前
大前端手里的 Next.js:从 #root 到 SEO,一份 HTML 的两种命运
前端·后端
IT_陈寒9 分钟前
Vue的v-for不听话?我被这个Key的坑整懵了
前端·人工智能·后端
2601_9638699516 分钟前
【计算机毕业设计】基于Java的相框定制系统的设计与实现
java·开发语言·课程设计
众人皆醒我独醉33 分钟前
训练加速实战:Flash Attention、Gradient Checkpointing 与数据流水线
后端·面试·gpu
阿黎梨梨38 分钟前
Next.js 全栈开发:从 SPA 的痛点到 SSR 的破局之道
前端·后端
名字还没想好☜1 小时前
Go 的 unsafe.Pointer 实战:零拷贝 []byte↔string 转换与三条铁律
开发语言·后端·golang·go·unsafe
老孙讲技术1 小时前
周末档期厨房爆单,带宽账单也爆了——不是直播难,是主码流 + always 计划在烧钱
后端·物联网
于烛1 小时前
为什么hasNext() 对只有一个元素的集合返回 true?90%初学者都有的疑问
java
老孙讲技术1 小时前
校园开放日前夜才说要「全校透明」?我用轻应用把 9 路教室预览和回放嵌进了校园后台
后端·物联网
wuminyu1 小时前
JVM利用io_uring优化堆外内存性能原理剖析
java·linux·c语言·jvm·c++