1. 程式人生 > >bolb:http 加密視訊路徑

bolb:http 加密視訊路徑

後臺程式碼,僅需讀取檔案並變成二進位制流: /** * bolb 測試 */

public void getFile(HttpServletResponse response, HttpServletRequest request){
    File f = new File("service-web/src/main/resources/static/images/video1.mp4");

    String fileName = f.getName();
    String agent = request.getHeader("User-Agent").toUpperCase();

    InputStream fis = null;

    OutputStream os = null;
    try{

        fis = new BufferedInputStream(new FileInputStream(f.getPath()));

        byte[] buffer;

        buffer = new byte[fis.available()];

        fis.read(buffer);

        response.reset();

        //由於火狐和其他瀏覽器顯示名稱的方式不相同,需要進行不同的編碼處理

        if(agent.indexOf("FIREFOX") != -1){//火狐瀏覽器

            response.addHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes("GB2312"),"ISO-8859-1"));

        }else{//其他瀏覽器

            response.addHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));

        }

        //設定response編碼

        response.setCharacterEncoding("UTF-8");

        response.addHeader("Content-Length", ""+ f.length());

        //設定輸出檔案型別

        response.setContentType("video/mpeg4");

        //獲取response輸出流

        os = response.getOutputStream();

        // 輸出檔案

        os.write(buffer);

    }catch(Exception e){

        System.out.println(e.getMessage());

    } finally {

        //關閉流

        try {

            if (fis != null) {

                fis.close();

            }

        } catch (IOException e) {

            System.out.println(e.getMessage());

        } finally {

            try {

                if (os != null) {

                    os.flush();

                }

            } catch (IOException e) {

                System.out.println(e.getMessage());

            } finally {

                try {

                    if (os != null) {

                        os.close();

                    }

                } catch (IOException e) {

                    System.out.println(e.getMessage());

                }

            }
        }
    }
}

前臺接收程式碼:

function demoBlob() {
	//建立XMLHttpRequest物件
    var xhr = new XMLHttpRequest();

    //配置請求方式、請求地址以及是否同步

    xhr.open('POST', '/getBolb', true);

    //設定請求結果型別為blob

    xhr.responseType = 'blob';

    //請求成功回撥函式

    xhr.onload = function (e) {
        if (this.status == 200) {//請求成功

            //獲取blob物件

            var blob = this.response;

            //獲取blob物件地址,並把值賦給容器
            console.log("!_______________________________________________________________________________________________")
            console.log("加密後的視訊路徑:"+URL.createObjectURL(blob));
            console.log("!_______________________________________________________________________________________________")

        }

    };

    xhr.send();
}