获取所有的mp4在线播放地址,并当前端调用该接口时返回mp4视频文件名及其播放地址,mp4视频存放在D盘的video文件夹下
@ApiOperation("获取所有MP4播放地址")
@GetMapping("/get/video/address")
public List<Map<String, String>> getVideoAddress() {
File file = new File("D:\\video");
List<Map<String, String>> fileNs = new ArrayList<Map<String, String>>();
String filename = "";
File[] subFile = ();
for (File value : subFile) {
// 判断是否为文件夹
if (!()) {
Map<String, String> jsonVideos = new HashMap<>();
String subFileName = ();
// 判断是否为mp4结尾
if (().toLowerCase().endsWith(".mp4")) {
filename = "http://localhost:8080/video/alone/video/play" + subFileName;
String chFileName = VIDEO_EN_CH.get(subFileName);
("filename",chFileName);
("address",filename);
(jsonVideos);
}
}
}
return fileNs;
}
单个视频在线播放接口,这里需要传mp4文件名
@ApiOperation("单个MP4播放")
@GetMapping(value = "/alone/video/play/{filename}" ,produces ="application/json;charset=utf-8")
public void aloneVideoPlay(HttpServletRequest request, @PathVariable("filename") String fileName, HttpServletResponse response) {
InputStream is = null;
OutputStream os = null;
try {
("video/mp4");
File file = new File("D:\\video\\" + fileName);
("Content-Length", "" + ());
is = new FileInputStream(file);
os = ();
(is, os);
} catch (Exception e) {
("播放MP4失败", e);
} finally {
if (null != os) {
try {
();
} catch (IOException e) {
();
}
}
}
}
完整Controller
@Api(tags = "视频Controller")
@RestController
@RequestMapping("/video")
public class VideoController {
@ApiOperation("获取所有MP4播放地址")
@GetMapping("/get/video/address")
public List<Map<String, String>> getVideoAddress() {
File file = new File("D:\\video");
List<Map<String, String>> fileNs = new ArrayList<Map<String, String>>();
String filename = "";
File[] subFile = ();
for (File value : subFile) {
// 判断是否为文件夹
if (!()) {
Map<String, String> jsonVideos = new HashMap<>();
String subFileName = ();
// 判断是否为mp4结尾
if (().toLowerCase().endsWith(".mp4")) {
filename = "http://localhost:8080/video/alone/video/play" + subFileName;
String chFileName = VIDEO_EN_CH.get(subFileName);
("filename",chFileName);
("address",filename);
(jsonVideos);
}
}
}
return fileNs;
}
@ApiOperation("单个MP4播放")
@GetMapping(value = "/alone/video/play/{filename}" ,produces ="application/json;charset=utf-8")
public void aloneVideoPlay(HttpServletRequest request, @PathVariable("filename") String fileName, HttpServletResponse response) {
InputStream is = null;
OutputStream os = null;
try {
("video/mp4");
File file = new File("D:\\video\\" + fileName);
("Content-Length", "" + ());
is = new FileInputStream(file);
os = ();
(is, os);
} catch (Exception e) {
("播放MP4失败", e);
} finally {
if (null != os) {
try {
();
} catch (IOException e) {
();
}
}
}
}
}
扩展对视频进行分页
PageUtil
public class PageUtil {
/**
* 开始分页
* @param list
* @param pageNum 页码
* @param pageSize 每页多少条数据
* @return
*/
public static List startPage(List list, Integer pageNum,
Integer pageSize) {
if (list == null) {
return null;
}
if (() == 0) {
return null;
}
Integer count = (); // 记录总数
Integer pageCount = 0; // 页数
if (count % pageSize == 0) {
pageCount = count / pageSize;
} else {
pageCount = count / pageSize + 1;
}
int fromIndex = 0; // 开始索引
int toIndex = 0; // 结束索引
if (pageNum != pageCount) {
fromIndex = (pageNum - 1) * pageSize;
toIndex = fromIndex + pageSize;
} else {
fromIndex = (pageNum - 1) * pageSize;
toIndex = count;
}
List pageList = (fromIndex, toIndex);
return pageList;
}
}
获取所有MP4播放地址(分页),需要前端调该接口时,传两个参数,pageNum及pageSize,页码和每页多少条
@ApiOperation("获取所有MP4播放地址")
@PostMapping("/get/video/address")
public List<Map<String, String>> getVideoAddress(@RequestBody Map<String, String> json) {
File file = new File("D:\\video");
List<Map<String, String>> fileNs = new ArrayList<Map<String, String>>();
String filename = "";
File[] subFile = ();
for (File value : subFile) {
// 判断是否为文件夹
if (!()) {
Map<String, String> jsonVideos = new HashMap<>();
String subFileName = ();
// 判断是否为mp4结尾
if (().toLowerCase().endsWith(".mp4")) {
filename = "http://localhost:8080/video/alone/video/play" + subFileName;
String chFileName = VIDEO_EN_CH.get(subFileName);
("filename",chFileName);
("address",filename);
(jsonVideos);
}
}
}
String pageNum = ("pageNum");
String pageSize = ("pageSize");
return (fileNs, (pageNum), (pageSize));
}