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

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

问题描述

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



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

所以,有没有办法解决这个问题?我在某处丢失了什么?



视频文件与REST界面位于同一服务器,REST界面仅检查会话并发送视频找出它应该发送哪一个。



这些是我迄今为止尝试过的方法。他们都工作,但他们都没有导航。



方法1,ResponseEntity:



  / * 
*这实际上将整个视频文件加载到内存中的一个字节数组中,因此不建议使用
*。
* /
@RequestMapping(value =/ {id} / preview,method = RequestMethod.GET)
@ResponseBody public ResponseEntity< byte []> getPreview1(@PathVariable(id)String id,HttpServletResponse响应){
ResponseEntity< byte []> result = null;
尝试{
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());
}
返回结果;
}



方法2,流复制:



  / * 
* IOUtils可用于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());




$ h $方法3,FileSystemResource:

  @RequestMapping(value =/ {id} / preview3,method = RequestMethod.GET)
@ResponseBody public FileSystemResource getPreview3(@PathVariable (id)String id,HttpServletResponse响应){
String path = repositoryService.findVideoLocationById(id);
返回新的FileSystemResource(路径);
}


解决方案

成为你的朋友。我以前有同样的问题。在实现http范围后,视频中的导航是可能的:

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


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.

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?

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.

Method 1, ResponseEntity:

/*
 * 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;
}

Method 2, Stream copy:

/*
 * 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());
    }
}

Method 3, FileSystemResource:

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

解决方案

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天全站免登陆