如何使用 Spring MVC 返回视频,以便可以使用 html5 <video> 进行导航标签? [英] How do I return a video with Spring MVC so that it can be navigated using the html5 <video> tag?

查看:22
本文介绍了如何使用 Spring MVC 返回视频,以便可以使用 html5 <video> 进行导航标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 Web 服务器 (Tomcat) 中有一个文件并创建了一个标签,我就可以观看视频、暂停它、浏览它,并在它完成后重新启动它.

If I have a file in the web server (Tomcat) and create a tag, I can watch the video, pause it, navigate through it, and restart it after it finishes.

但如果我创建一个 REST 接口,在请求时发送视频文件,并将其 URL 添加到标签,我只能播放和暂停.没有倒带,没有快进,没有导航,什么都没有.

But if I create a REST interface that sends the video file when requested, and add its URL to a tag, I can only play and pause. No rewinding, no fast forward, no navigating, nothing.

那么,有没有办法解决这个问题?我是不是在某处遗漏了什么?

So, is there a way for this to be fixed? Am I missing something somewhere?

视频文件和REST接口在同一个服务器,REST接口只检查session,在确定应该发送哪个视频后发送.

Video files are in the same server as the REST interface, and the REST interface only checks session and sends the video after finding out which one it should send.

这些是我迄今为止尝试过的方法.它们都可以工作,但都不允许导航.

These are the methods I've tried so far. They all work, but none of them allow navigating.

/*
 * This will actually load the whole video file in a byte array in memory,
 * so it's not recommended.
 */
@RequestMapping(value = "/{id}/preview", method = RequestMethod.GET)
@ResponseBody public ResponseEntity<byte[]> getPreview1(@PathVariable("id") String id, HttpServletResponse response) {
    ResponseEntity<byte[]> result = null;
    try {
        String path = repositoryService.findVideoLocationById(id);
        Path path = Paths.get(pathString);
        byte[] image = Files.readAllBytes(path);

        response.setStatus(HttpStatus.OK.value());
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentLength(image.length);
        result = new ResponseEntity<byte[]>(image, headers, HttpStatus.OK);
    } catch (java.nio.file.NoSuchFileException e) {
        response.setStatus(HttpStatus.NOT_FOUND.value());
    } catch (Exception e) {
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
    }
    return result;
}

方法二,流复制:

/*
 * IOUtils is available in Apache commons io
 */
@RequestMapping(value = "/{id}/preview2", method = RequestMethod.GET)
@ResponseBody public void getPreview2(@PathVariable("id") String id, HttpServletResponse response) {
    try {
        String path = repositoryService.findVideoLocationById(id);
        File file = new File(path)
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setHeader("Content-Disposition", "attachment; filename="+file.getName().replace(" ", "_"));
        InputStream iStream = new FileInputStream(file);
        IOUtils.copy(iStream, response.getOutputStream());
        response.flushBuffer();
    } catch (java.nio.file.NoSuchFileException e) {
        response.setStatus(HttpStatus.NOT_FOUND.value());
    } catch (Exception e) {
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
    }
}

方法 3,文件系统资源:

@RequestMapping(value = "/{id}/preview3", method = RequestMethod.GET)
@ResponseBody public FileSystemResource getPreview3(@PathVariable("id") String id, HttpServletResponse response) {
    String path = repositoryService.findVideoLocationById(id);
    return new FileSystemResource(path);
}

推荐答案

HTTP 恢复下载功能可能是你的朋友.我以前也遇到过同样的问题.实现 http 范围后,视频中的导航是可能的:

The HTTP resume download function might be your friend. I had the same problem before. After implementing http range the navigation in the video was possible:

http://balusc.blogspot.com/2009/02/fileservlet-supporting-resume-and.html

这篇关于如何使用 Spring MVC 返回视频,以便可以使用 html5 &lt;video&gt; 进行导航标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆