接口写好了吗 2022-03-28 11:00 采纳率: 80.2%
浏览 971
已结题

java如何把多个视频文件以流的形式传给前端

我现在是可以根据一个视频的地址拿到对应的数据转成流输出给前端展示。但是如何多个视频一起传过去呢,代码是这样。
1,返回视频流的

package com.example.demo.utils;

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;
import java.nio.file.Path;
//返回视频流

@Component
public class NonStaticResourceHttpRequestHandler extends ResourceHttpRequestHandler {
    public final static String ATTR_FILE = "NON-STATIC-FILE";

    @Override
    protected Resource getResource(HttpServletRequest request) {
        final Path filePath = (Path) request.getAttribute(ATTR_FILE);
        return new FileSystemResource(filePath);
    }

}


2,controller

package com.example.demo.controller;


import com.example.demo.mapper.VideoUploadMapper;
import com.example.demo.utils.NonStaticResourceHttpRequestHandler;

import lombok.AllArgsConstructor;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
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;

@RestController
@RequestMapping("/SelectVideo")
@AllArgsConstructor
public class SelectVideoController {
    private final NonStaticResourceHttpRequestHandler nonStaticResourceHttpRequestHandler;
    @Resource
    VideoUploadMapper videoUploadMapper;
    @CrossOrigin(origins = "*", maxAge = 3600)
    @GetMapping("/policemen")
    public void videoPreview(HttpServletRequest request, HttpServletResponse response) throws Exception {

        //视频的地址
        String realPath = "D:/video/videoUpload/beae15e0ae3345eb8ce3f54636b295c9.mp4";

        Path filePath = Paths.get(realPath );
        //Files.exists:用来测试路径文件是否存在
        if (Files.exists(filePath)) {
            //获取视频的类型
            String mimeType = Files.probeContentType(filePath);
            if (StringUtils.hasText(mimeType)) {
                //判断类型,根据不同的类型文件来处理对应的数据
                response.setContentType(mimeType);
            }
            request.setAttribute(NonStaticResourceHttpRequestHandler.ATTR_FILE, filePath);
            nonStaticResourceHttpRequestHandler.handleRequest(request, response);
        } else {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        }
    }

}


前端的代码video

          <video
            src="http://localhost:8001/SelectVideo/policemen"
            controls="controls"
            width="100%"
            id="myvideo"
          >
          </video>

  • 写回答

1条回答 默认 最新

  • 一起随缘 2022-03-28 11:41
    关注

    可以将多个视频对应的流信息放到list或map中,然后前端拿到每一个流信息就行处理

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 7月1日
  • 已采纳回答 6月23日
  • 创建了问题 3月28日

悬赏问题

  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺