为什么java流视频不是在crome浏览器中播放,而是在firefox中播放

时间:2017-11-16 07:04:45

标签: java spring-mvc video-streaming

@RequestMapping(value = "/video/{clientID}/{fileName}", method = RequestMethod.GET)
public ResponseEntity<StreamingResponseBody> getClientVideo(@PathVariable(value = "clientID") Integer clientID, @PathVariable(value = "fileName") final String fileName) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    String absolutePath = new File(".").getAbsolutePath();
    File file = new File(Paths.get(absolutePath).getParent() + "/" + clientID);
    if (null != file) {
        FilenameFilter beginswithm = new FilenameFilter() {
            public boolean accept(File directory, String filename) {
                return filename.contains("ClientVideo_"+fileName);
            }
        };
        File[] files = file.listFiles(beginswithm);
        if (null != files && files.length > 0) {
            Resource resource = null;
            for (final File f : files) {
                headers.set("Content-Disposition", "inline; filename=" + f.getName());

                StreamingResponseBody responseBody = new StreamingResponseBody() {
                    @Override
                    public void writeTo(OutputStream out) throws IOException {
                        out.write(Files.readAllBytes(f.toPath()));
                        out.flush();
                        try {
                            Thread.sleep(5);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                };
                return ResponseEntity.ok().headers(headers).contentType(MediaType.APPLICATION_OCTET_STREAM).body(responseBody); //(responseBody, headers, HttpStatus.OK);
            }
        }
    }
    RecruiterResponseBean resBean = new RecruiterResponseBean();
    resBean.setStatusMessage("Video is not present : " + Constants.FAILED);
    resBean.setStatusCode(Constants.FAILED_CODE);
    return new ResponseEntity(HttpStatus.NOT_FOUND);
}

我正在使用Spring MVC,我尝试了很多方法,但都在下载视频。上面的代码是流视频,问题是:在Firefox中它是流媒体但在crome它正在下载。我想在crome中播放流媒体视频,如下所示。 [前视频直播视频:http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4]

我是否需要添加更多标题? [如果是这样,哪些标题需要添加?] 或者我错过了什么?

0 个答案:

没有答案