Java 使用blob對H5視頻播放進行加密

來源:互聯網
上載者:User

標籤:ESS   top   etc   string   map   tps   結果   XML   print   

1.建立一個H5 <video>標籤

<video id="sound" type="video/mp4" controls="controls" autoplay="autoplay"       webkit-playsinline="true"    playsinline="true"   heigth="100%"></video>

 

2.建立請求擷取視頻源,並將視頻源轉為blob對象

    //建立XMLHttpRequest對象    var xhr = new XMLHttpRequest();    //配置請求方式、請求地址以及是否同步    xhr.open(‘POST‘, ‘/armystudy/blob/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();

 

3.Java後台將視頻轉為視頻源

   @ResponseBody    @RequestMapping("/getVideoSrc")    public OutputStream getVideoSrc(HttpServletRequest httpServletRequest,                                    HttpServletResponse httpServletResponse){        //1.建立檔案對象        File f = new File("E:/test/1.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.由於Firefox和其他瀏覽器顯示名稱的方式不相同,需要進行不同的編碼處理            if(agent.indexOf("FIREFOX") != -1){//Firefox瀏覽器                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(); }            } catch (IOException e) {                System.out.println(e.getMessage());            }        }        return os;    }

 

Java 使用blob對H5視頻播放進行加密

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.