今天發現慕課網中的視頻播放地址使用了Blob加密。這是一種新的用法,我是第一次發現。因此便研究了一下它的用法。
採用Blob可以在一定程度上模糊住大家。例如下面的這個播放地址:
blob:http://simpl.info/884da595-7816-4211-b6c3-607c444556ef
BLOB (binary large object),二進制大對象,是一個可以存儲二進制文件的容器。Video 使用 blob 二進制流需要前後端同時支持。
Java 生成 Blob 二進制流
-
/*
-
* 在這裏可以進行權限驗證等操作
-
*/
-
//創建文件對象
-
File f = new File("E:\\test.mp4");
-
//獲取文件名稱
-
String fileName = f.getName();
-
//導出文件
-
String agent = getRequest().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);
-
getResponse().reset();
-
//由於火狐和其他瀏覽器顯示名稱的方式不相同,需要進行不同的編碼處理
-
if(agent.indexOf("FIREFOX") != -1){//火狐瀏覽器
-
getResponse().addHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes("GB2312"),"ISO-8859-1"));
-
}else{//其他瀏覽器
-
getResponse().addHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));
-
}
-
//設置response編碼
-
getResponse().setCharacterEncoding("UTF-8");
-
getResponse().addHeader("Content-Length", "" + f.length());
-
//設置輸出文件類型
-
getResponse().setContentType("video/mpeg4");
-
//獲取response輸出流
-
os = getResponse().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());
-
}
-
}
-
}
-
}
HTML5 Video 使用 Blob
-
//創建XMLHttpRequest對象
-
var xhr = new XMLHttpRequest();
-
//配置請求方式、請求地址以及是否同步
-
xhr.open('POST', './play', 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();
HTML代碼:
1 |
|