目前主流的就是后台读取文件,将文件以流的形式返回前台,直接上代码
1.前台代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SpringBoot文件页面</title>
</head>
<body>
<video class="box" align="center" controls="controls" width="350px" heigth="350px" autoplay="autoplay">
<source src="http://localhost:8087/demo/video" type="video/mp4" class="">
</video>
</body>
2.后台代码
@GetMapping("/video")
public String video(HttpServletRequest request , HttpServletResponse response) throws Exception {
File file = new File("D:\\资料\\接着奏乐,接着舞.mp4");
//读取指定路径下面的视频
InputStream in = new FileInputStream(file);
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
//创建存放视频内容的数组
byte[] buff =new byte[1024];
//所读取的内容使用n来接收
int n;
//当没有读取完时,继续读取,循环
while((n=in.read(buff))!=-1){
//将字节数组的数据全部写入到输出流中
outputStream.write(buff,0,n);
}
//强制将缓存区的数据进行输出
outputStream.flush();
//关流
outputStream.close();
in.close();
return "ok";
}
3.效果图
谢谢观看!