前端 src:视频请求路径
<div class="shiping">
<video id="video" class="video-js vjs-big-play-centered" controls="controls" poster="">
<source src="/paper/questions/getVideo" type='video/mp4'>
</video>
</div>
后端读取视频返回给前端
/**
* 获取视屏
*/
@GetMapping("/paper/questions/getVideo")
@ResponseBody
public void getVideo(HttpServletRequest request, HttpServletResponse response)
{
//视频资源存储信息
response.reset();
//获取从那个字节开始读取文件
String rangeString = request.getHeader("Range");
try {
//获取响应的输出流
OutputStream outputStream = response.getOutputStream();
//读取本地视频
File file = new File("C:\\Users\\cygg\\Desktop\\exam.mp4");
if(file.exists()){
RandomAccessFile targetFile = new RandomAccessFile(file, "r");
long fileLength = targetFile.length();
//播放
if(rangeString != null){
long range = Long.valueOf(rangeString.substring(rangeString.indexOf("=") + 1, rangeString.indexOf("-")));
//设置内容类型
response.setHeader("Content-Type", "video/mp4");
//设置此次相应返回的数据长度
response.setHeader("Content-Length", String.valueOf(fileLength - range));
//设置此次相应返回的数据范围
response.setHeader("Content-Range", "bytes "+range+"-"+(fileLength-1)+"/"+fileLength);
//返回码需要为206,而不是200
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
//设定文件读取开始位置(以字节为单位)
targetFile.seek(range);
}else {//下载
//设置响应头,把文件名字设置好
response.setHeader("Content-Disposition", "attachment; filename=video.mp4" );
//设置文件长度
response.setHeader("Content-Length", String.valueOf(fileLength));
//解决编码问题
response.setHeader("Content-Type","application/octet-stream");
}
byte[] cache = new byte[1024 * 300];
int flag;
while ((flag = targetFile.read(cache))!=-1){
outputStream.write(cache, 0, flag);
}
}else {
String message = "file: not exists";
//解决编码问题
response.setHeader("Content-Type","application/json");
outputStream.write(message.getBytes(StandardCharsets.UTF_8));
}
outputStream.flush();
outputStream.close();
} catch (Exception e) {
}
}