SpringBoot接口实现视频在线播放

 获取所有的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 = file.listFiles();
        for (File value : subFile) {
            // 判断是否为文件夹
            if (!value.isDirectory()) {
                Map<String, String> 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<Map<String, String>> getVideoAddress() {
        File file = new File("D:\\video");
        List<Map<String, String>> fileNs = new ArrayList<Map<String, String>>();
        String filename = "";
        File[] subFile = file.listFiles();
        for (File value : subFile) {
            // 判断是否为文件夹
            if (!value.isDirectory()) {
                Map<String, String> 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 {

    /**
     * 开始分页
     * @param list
     * @param pageNum 页码
     * @param pageSize 每页多少条数据
     * @return
     */
    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<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 = file.listFiles();
        for (File value : subFile) {
            // 判断是否为文件夹
            if (!value.isDirectory()) {
                Map<String, String> 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));
    }
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

像向日葵一样~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值