java 如何设置.mp4的文件链接为blob:http://加密的格式

前台代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<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', '/Today/VideoServlet', 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>

后台代码

package cn.com.servlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class VideoServlet extends HttpServlet {
	/*
	 * author:命运的信徒
	 * arm:把.mp4结尾的视频转换为blob:http://加密的形式
	 */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //加载文件的位置
    	File file = new File("C:\\Users\\Administrator\\Desktop\\one\\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();
            }
        }

    }
}


页面效果

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_37591637

请给我持续更新的动力~~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值