java 使用 aws s3 sdk 通过分段下载来实现 html 页面 video 的断点播放、拖动进度播放

参考博客:

  1. Java 视频流分段返回

  2. java 拉取 aws s3 视频流返回给浏览器:

     @Override
     public void playVideo(Long fileLength,String key,HttpServletRequest request,HttpServletResponse response) {
         OutputStream outputStream = null;
         S3ObjectInputStream s3ObjectInputStream = null;
         try {
             outputStream = response.getOutputStream();
    
             // 获取从哪个字节开始读取文件
             String rangeHeader = request.getHeader("Range");
             if (rangeHeader == null) {
                 response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Required 'Range' header is missing");
                 return;
             }
    
             long startByte = 0;
             long endByte = fileLength - 1;
             if (rangeHeader.startsWith("bytes=")) {
                 String[] rangeParts = rangeHeader.substring(6).split("-");
                 startByte = Long.parseLong(rangeParts[0]);
                 endByte = rangeParts.length > 1 ? startByte : fileLength - 1;
             }
    
             if (startByte > endByte || startByte < 0 || endByte >= fileLength) {
                 response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE, "Requested range not satisfiable");
                 return;
             }
    
             // 设置响应头
             response.setHeader("Content-Type", "video/mp4");
             response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
             response.setHeader("Content-Range", "bytes " + startByte + "-" + endByte + "/" + fileLength);
    
             // 从S3获取视频流
             GetObjectRequest getObjectRequest = new GetObjectRequest(this.fileBucket, key).withRange(startByte, endByte + 1);
             S3Object s3Object = this.amazonS3.getObject(getObjectRequest);
             s3ObjectInputStream = s3Object.getObjectContent();
    
             // 将视频流响应给客户端
             byte[] buffer = new byte[1024 * 1024]; // 使用1MB的缓冲区
             int bytesRead;
             long totalBytesRead = 0;
             while ((bytesRead = s3ObjectInputStream.read(buffer)) != -1 && totalBytesRead <= (endByte - startByte + 1)) {
                 outputStream.write(buffer, 0, bytesRead);
                 totalBytesRead += bytesRead;
             }
             response.setContentLength((int) (endByte - startByte + 1)); // 设置实际返回的数据长度
         } catch (Exception e) {
             // 处理异常
             log.error( CommonConstans.EXCEPTION_LOG_PREFIX,e );
             // 处理异常
             try {
                 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Internal server error");
             }catch ( Exception e ){
                 log.error( CommonConstans.EXCEPTION_LOG_PREFIX,e );
             }
         }finally {
             if( outputStream != null ){
                 try {
                     outputStream.close();
                 }catch ( Exception e1 ){
                     log.error( CommonConstans.EXCEPTION_LOG_PREFIX,e1 );
                 }
             }
             if( s3ObjectInputStream != null ){
                 try {
                     s3ObjectInputStream.close();
                 }catch ( Exception e1 ){
                     log.error( CommonConstans.EXCEPTION_LOG_PREFIX,e1 );
                 }
             }
         }
     }
    
  3. java读物本地视频文件返回给浏览器:

java 复制代码
    @Override
    public void playVideo(HttpServletRequest request, HttpServletResponse response) {
        RandomAccessFile randomAccessFile = null;
        OutputStream outputStream = null;
        try {
            response.reset();
            // File file = new File("D:\\素材\\视频\\crazy_max_small.mp4");
            File file = new File("D:\\视频\\888.mp4");
            long fileLength = file.length();
            // 随机读文件
            randomAccessFile = new RandomAccessFile(file, "r");

            //获取从那个字节开始读取文件
            String rangeString = request.getHeader("Range");
            System.out.println(rangeString);
            long range=0;
            if (rangeString!=null) {
                range = Long.valueOf(rangeString.substring(rangeString.indexOf("=") + 1, rangeString.indexOf("-")));
            }
            //获取响应的输出流
            outputStream = response.getOutputStream();
            //设置内容类型
            response.setHeader("Content-Type", "video/mp4");
            //返回码需要为206,代表只处理了部分请求,响应了部分数据
            response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);

            // 移动访问指针到指定位置
            randomAccessFile.seek(range);
            // 每次请求只返回1MB的视频流
            byte[] bytes = new byte[1024 * 1024];
            int len = randomAccessFile.read(bytes);
            //设置此次相应返回的数据长度
            response.setContentLength(len);
            //设置此次相应返回的数据范围
            response.setHeader("Content-Range", "bytes "+range+"-"+(fileLength-1)+"/"+fileLength);
            // 将这1MB的视频流响应给客户端
            outputStream.write(bytes, 0, len);
            System.out.println("返回数据区间:【"+range+"-"+(range+len)+"】");
        }catch ( Exception e ){
            log.error( CommonConstans.EXCEPTION_LOG_PREFIX,e );
            // 处理异常
            try {
                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Internal server error");
            }catch ( Exception e1 ){
                log.error( CommonConstans.EXCEPTION_LOG_PREFIX,e1 );
            }
        }finally {
            if( outputStream != null){
                try {
                    outputStream.close();
                }catch ( Exception e){
                    log.error( CommonConstans.EXCEPTION_LOG_PREFIX,e );
                }
            }
            if( randomAccessFile != null ){
                try {
                    randomAccessFile.close();
                }catch ( Exception e ){
                    log.error( CommonConstans.EXCEPTION_LOG_PREFIX,e );
                }
            }
        }
相关推荐
安小华0278 分钟前
html(超文本标记语言)
前端·html
从前有个范特西11 分钟前
酷黑金色配色 影片素材不过时 色彩丰富 电影主题html
前端·html
cloud___fly25 分钟前
Spring AOP入门
java·后端·spring
灰色孤星A1 小时前
瑞吉外卖项目学习笔记(四)@TableField(fill = FieldFill.INSERT)公共字段填充、启用/禁用/修改员工信息
java·学习笔记·springboot·瑞吉外卖·黑马程序员·tablefield·公共字段填充
逊嘘1 小时前
【Java数据结构】ArrayList相关的算法
java·开发语言
Y编程小白1 小时前
SpringBoot的创建方式
java·spring boot·后端
总是学不会.1 小时前
【集合】Java 8 - Stream API 17种常用操作与案例详解
java·windows·spring boot·mysql·intellij-idea·java集合
潜意识起点1 小时前
【潜意识Java】javaee中的SpringBoot在Java 开发中的应用与详细分析
java·spring boot·后端
mxbb.2 小时前
单点Redis所面临的问题及解决方法
java·数据库·redis·缓存