如果我在Web服务器(Tomcat)中有一个文件并创建标签,则可以观看视频,暂停视频,浏览视频并在完成后重新启动。
但是,如果我创建了一个REST接口,该接口在请求时发送视频文件,并将其URL添加到标签,则只能播放和暂停。没有倒带,没有快进,没有导航,什么也没有。
那么,有什么办法可以解决此问题?我在某处缺少什么吗?
视频文件与REST接口位于同一服务器中,并且REST接口在确定应发送的视频之后,仅检查会话并发送视频。
这些是我到目前为止尝试过的方法。它们都起作用,但是它们都不允许导航。
方法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; }
方法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()); } }
方法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); }
处理非静态资源的简单解决方案:
@SpringBootApplication public class DemoApplication { private final static File MP4_FILE = new File("/home/ego/bbb_sunflower_1080p_60fps_normal.mp4"); public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Controller final static class MyController { @Autowired private MyResourceHttpRequestHandler handler; // supports byte-range requests @GetMapping("/") public void home( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { request.setAttribute(MyResourceHttpRequestHandler.ATTR_FILE, MP4_FILE); handler.handleRequest(request, response); } // does not support byte-range requests @GetMapping(path = "/plain", produces = "video/mp4") public FileSystemResource plain() { return new FileSystemResource(MP4_FILE); } } @Component final static class MyResourceHttpRequestHandler extends ResourceHttpRequestHandler { private final static String ATTR_FILE = MyResourceHttpRequestHandler.class.getName() + ".file"; @Override protected Resource getResource(HttpServletRequest request) throws IOException { final File file = (File) request.getAttribute(ATTR_FILE); return new FileSystemResource(file); } } }
(受Spring Boots LogFileMvcEndpoint的启发,并且或多或少等于我稍后发现的Paul-Warrens(@ paul-warren)StoreByteRangeHttpRequestHandler)。