zoukankan      html  css  js  c++  java
  • 使用blob对H5视频播放进行加密

    package com.winxur.controller;
    
    import org.apache.commons.io.IOUtils;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.MediaType;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.io.*;
    import java.net.URLEncoder;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @RequestMapping(value = "redio")
    @Controller
    public class RedioController {
        /**
         * video 视频流一
         *
         * @param request
         * @param response
         */
        @RequestMapping(value = "/getVido", method = RequestMethod.GET)
        @ResponseBody
        public void getVido(HttpServletRequest request, HttpServletResponse response) {
            String file = "Av706.mp4";
            try {
                FileInputStream inputStream = new FileInputStream(file);
                byte[] data = new byte[inputStream.available()];
                inputStream.read(data);
                String diskfilename = "final.mp4";
                response.setContentType("video/mp4");
                response.setHeader("Content-Disposition", "attachment; filename="" + diskfilename + """);
                System.out.println("data.length " + data.length);
                response.setContentLength(data.length);
                response.setHeader("Content-Range", "" + Integer.valueOf(data.length - 1));
                response.setHeader("Accept-Ranges", "bytes");
                response.setHeader("Etag", "W/"9767057-1323779115364"");
                OutputStream os = response.getOutputStream();
    
                os.write(data);
                //先声明的流后关掉!
                os.flush();
                os.close();
                inputStream.close();
    
            } catch (Exception e) {
    
            }
        }
        /**
         * video 视频流二
         *
         * IOUtils is available in Apache commons io
         */
        @RequestMapping(value = "/preview2", method = RequestMethod.GET)
        @ResponseBody
        public void getPreview2( HttpServletResponse response) {
            try {
                File file = new File("Av706.mp4");
                response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
                response.setHeader("Content-Disposition", "attachment; filename="+file.getName().replace(" ", "_"));
                InputStream iStream = new FileInputStream(file);
                IOUtils.copy(iStream, response.getOutputStream());
                response.flushBuffer();
            } catch (java.nio.file.NoSuchFileException e) {
                response.setStatus(HttpStatus.NOT_FOUND.value());
            } catch (Exception e) {
                response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            }
        }
    
        @ResponseBody
        @RequestMapping("/getVideoSrc")
        public OutputStream getVideoSrc(HttpServletRequest httpServletRequest,
                                        HttpServletResponse httpServletResponse){
            //1.创建文件对象
            File f = new File("Av706.mp4");
            //2.获取文件名称
            String fileName = f.getName();
            //3.导出文件
            String agent = httpServletRequest.getHeader("User-Agent").toUpperCase();
            InputStream fis = null;
            OutputStream os = null;
            try {
                //4.获取输入流
                fis = new BufferedInputStream(new FileInputStream(f.getPath()));
                byte[] buffer;
                buffer = new byte[fis.available()];
                fis.read(buffer);
                httpServletResponse.reset();
                //5.由于火狐和其他浏览器显示名称的方式不相同,需要进行不同的编码处理
                if(agent.indexOf("FIREFOX") != -1){//火狐浏览器
                    httpServletResponse.addHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes("GB2312"),"ISO-8859-1"));
                }else{//其他浏览器
                    httpServletResponse.addHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));
                }
                //6.设置response编码
                httpServletResponse.setCharacterEncoding("UTF-8");
                httpServletResponse.addHeader("Content-Length", "" + f.length());
                //设置输出文件类型
                httpServletResponse.setContentType("video/mpeg4");
                //7.获取response输出流
                os = httpServletResponse.getOutputStream();
                os.flush();
                //8.输出文件
                os.write(buffer);
            }catch(Exception e){
                System.out.println(e.getMessage());
            } finally{
                //关闭流
                try {
                    if(fis != null){ fis.close(); }
    
                    if(os != null){ os.flush(); }
    
                    if(os != null){os.close(); }
                    return null;
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
    
            return os;
        }
        //跳转html
        @RequestMapping(value ="/hello")
        public String index() {
            return "hello";
        }
    }
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8" />
        <title>Insert title here</title>
    </head>
    <!--JQuery在线引用-->
    <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js"></script>
    
    <body>
    <video id="sound" type="video/mp4" controls="controls" autoplay="autoplay"
           webkit-playsinline="true"    playsinline="true"   heigth="100%"></video>
    
    
    <script>
        //创建XMLHttpRequest对象
        var xhr = new XMLHttpRequest();
        //配置请求方式、请求地址以及是否同步
        xhr.open('POST', '/redio/getVideoSrc', true);
        //设置请求结果类型为blob
        xhr.responseType = 'blob';
        //请求成功回调函数
        xhr.onload = function(e) {
            if (this.status == 200) {//请求成功
                //获取blob对象
                var blob = this.response;
                //获取blob对象地址,并把值赋给容器
                $("#sound").attr("src", URL.createObjectURL(blob));
            }
        };
        xhr.send();
    </script>
    </body>
    </html>
  • 相关阅读:
    python生成CSV文件并发送邮件
    Live2d Test Env
    Live2d Test Env
    Live2d Test Env
    Live2d Test Env
    Live2d Test Env
    Live2d Test Env
    Live2d Test Env
    Live2d Test Env
    扔鸡蛋
  • 原文地址:https://www.cnblogs.com/kjtt/p/14450714.html
Copyright © 2011-2022 走看看