html5 video标签播放视频流

从文件服务器读取音视频文件,以流的方式传给前台,并可以播放视频。html

作了一个demo,用html5的video,audio标签实现。前端

后台实现代码:html5

@GetMapping(value = "/getVideos")
public String getVideos(HttpServletRequest request, HttpServletResponse response)
{
    try {
        FileInputStream fis = null;
        OutputStream os = null ;
        fis = new FileInputStream("C:\\Users\\zhangxin\\Desktop\\douyin.mp4");
        int size = fis.available(); // 获得文件大小
        byte data[] = new byte[size];
        fis.read(data); // 读数据
        fis.close();
        fis = null;
        response.setContentType("video/mp4"); // 设置返回的文件类型
        os = response.getOutputStream();
        os.write(data);
        os.flush();
        os.close();
        os = null;
 
 
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

前端实现代码:服务器

<video width="1120" height="540" controls="controls" id="video" preload="auto"     >
    <source src="getVideos"   type="video/mp4">
</video>
相关文章
相关标签/搜索