版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qincidong/article/details/82781699
在知乎上看到一个视频,准备下载下来,结果下载不了,复制地址发现是blob://xxx。知乎帖子:https://www.zhihu.com/question/62753680/answer/382455062。
百度发现是对视频地址进行了blob加密,文章地址:https://blog.csdn.net/qq_36688143/article/details/79162013。
下面是使用Java Servlet+html5 video结合实现的一个对视频地址进行blob加密的示例。代码参考上文链接。
后台Servlet:
public class MyBlobVideoServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
File file = new File("C:\\Users\\Administrator\\Desktop\\102_d41c7dd69f5eba69800d5c3401a3c384_1.mp4");
String fileName = file.getName();
String userAgent = req.getHeader("User-Agent").toLowerCase();
if (userAgent.indexOf("firefox") != -1) {
resp.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("GB2312"), "ISO-8859-1"));
}
else {
resp.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
}
//设置response编码
resp.setCharacterEncoding("UTF-8");
resp.addHeader("Content-Length", "" + file.length());
//设置输出文件类型
resp.setContentType("video/mpeg4");
FileInputStream fis = null;
OutputStream os = null;
try {
//获取response输出流
os = resp.getOutputStream();
fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
// 输出文件
os.write(buffer,0,len);
}
} catch (Exception e) {
if (null != fis) {
fis.close();
}
if (null != os) {
os.flush();
os.close();
}
}
}
}
页面:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
</head>
<body>
<h2>blob video demo</h2>
<video id="sound" width="500" height="300" controls="controls"></video>
<script type="text/javascript">
//创建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对象地址,并把值赋给容器
document.getElementById('sound').src=URL.createObjectURL(blob);
}
};
xhr.send();
</script>
</body>
</html>
效果展示:
代码:https://gitee.com/qincd/my-test-projects下blob-video模块。
不过,知乎的对视频做了切割。