栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot接口实现视频在线播放

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot接口实现视频在线播放

 获取所有的mp4在线播放地址,并当前端调用该接口时返回mp4视频文件名及其播放地址,mp4视频存放在D盘的video文件夹下

    @ApiOperation("获取所有MP4播放地址")
    @GetMapping("/get/video/address")
    public List> getVideoAddress() {
        File file = new File("D:\video");
        List> fileNs = new ArrayList>();
        String filename = "";
        File[] subFile = file.listFiles();
        for (File value : subFile) {
            // 判断是否为文件夹
            if (!value.isDirectory()) {
                Map jsonVideos = new HashMap<>();
                String subFileName = value.getName();
                // 判断是否为mp4结尾
                if (subFileName.trim().toLowerCase().endsWith(".mp4")) {
                    filename = "http://localhost:8080/video/alone/video/play" + subFileName;
                    String chFileName = VIDEO_EN_CH.get(subFileName);
                    jsonVideos.put("filename",chFileName);
                    jsonVideos.put("address",filename);
                    fileNs.add(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 {
            response.setContentType("video/mp4");
            File file = new File("D:\video\" + fileName);
            response.addHeader("Content-Length", "" + file.length());
            is = new FileInputStream(file);
            os = response.getOutputStream();
            IOUtils.copy(is, os);
        } catch (Exception e) {
            log.error("播放MP4失败", e);
        } finally {
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

完整Controller

@Api(tags = "视频Controller")
@RestController
@RequestMapping("/video")
public class VideoController {
    
    @ApiOperation("获取所有MP4播放地址")
    @GetMapping("/get/video/address")
    public List> getVideoAddress() {
        File file = new File("D:\video");
        List> fileNs = new ArrayList>();
        String filename = "";
        File[] subFile = file.listFiles();
        for (File value : subFile) {
            // 判断是否为文件夹
            if (!value.isDirectory()) {
                Map jsonVideos = new HashMap<>();
                String subFileName = value.getName();
                // 判断是否为mp4结尾
                if (subFileName.trim().toLowerCase().endsWith(".mp4")) {
                    filename = "http://localhost:8080/video/alone/video/play" + subFileName;
                    String chFileName = VIDEO_EN_CH.get(subFileName);
                    jsonVideos.put("filename",chFileName);
                    jsonVideos.put("address",filename);
                    fileNs.add(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 {
            response.setContentType("video/mp4");
            File file = new File("D:\video\" + fileName);
            response.addHeader("Content-Length", "" + file.length());
            is = new FileInputStream(file);
            os = response.getOutputStream();
            IOUtils.copy(is, os);
        } catch (Exception e) {
            log.error("播放MP4失败", e);
        } finally {
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    
}

扩展对视频进行分页

        PageUtil

public class PageUtil {

    
    public static List startPage(List list, Integer pageNum,
                                 Integer pageSize) {
        if (list == null) {
            return null;
        }
        if (list.size() == 0) {
            return null;
        }

        Integer count = list.size(); // 记录总数
        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 = list.subList(fromIndex, toIndex);

        return pageList;
    }
}

获取所有MP4播放地址(分页),需要前端调该接口时,传两个参数,pageNum及pageSize,页码和每页多少条

    @ApiOperation("获取所有MP4播放地址")
    @PostMapping("/get/video/address")
    public List> getVideoAddress(@RequestBody Map json) {
        File file = new File("D:\video");
        List> fileNs = new ArrayList>();
        String filename = "";
        File[] subFile = file.listFiles();
        for (File value : subFile) {
            // 判断是否为文件夹
            if (!value.isDirectory()) {
                Map jsonVideos = new HashMap<>();
                String subFileName = value.getName();
                // 判断是否为mp4结尾
                if (subFileName.trim().toLowerCase().endsWith(".mp4")) {
                    filename = "http://localhost:8080/video/alone/video/play" + subFileName;
                    String chFileName = VIDEO_EN_CH.get(subFileName);
                    jsonVideos.put("filename",chFileName);
                    jsonVideos.put("address",filename);
                    fileNs.add(jsonVideos);
                }
            }
        }
        String pageNum = json.get("pageNum");
        String pageSize = json.get("pageSize");
        return PageUtil.startPage(fileNs, Integer.parseInt(pageNum), Integer.parseInt(pageSize));
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/735931.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号