分享

支持FLV视频在线快速拖放的java服务器端代码 - 战楠的日志 - 网易博客

 ShangShujie 2010-06-03

支持提交http://domain/flvfile.flv?show&start=number1&end=number2 参数拖放的FLV播放器,

修改播放器,支持播放时仅下载需要的几分钟视频,测试 地址:

http://www./flv/flv.html   http://www./flv/m4v.html  

flv文件需要经过FLVMDI等工具添加了“关键帧”对象。

支持FLV视频在线快速拖放的java服务器端代码,Servlet程序。

package org.baowei.cdn;

import java.io.RandomAccessFile;

import java.io.FileNotFoundException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.Cookie;

public class flv extends HttpServlet {

 public flv() {

 }

 public void doGet(HttpServletRequest request, HttpServletResponse response) {

  boolean rightCookie = false;

  Cookie[] cookies = request.getCookies();

  if (request.getHeader("x-flash-version") != null && cookies != null) {

   P: for (int i = 0; i < cookies.length; i++) {

    if (cookies[i].getName().startsWith("webMaster")) {

     // 防止盗链

     rightCookie = true;

     break P;

    }

   }

  }

  exit: if (rightCookie) {

   // format:

   // http://127.0.0.1:8080/cdn/flv?httpurl=http://www./flvfiles/a.flv?show&start=123&end=456

   String httpurl = "", localfile = "";

   int start = 0, end = 0;

   RandomAccessFile raf = null;

   try {

    httpurl = new String(request.getParameter("httpurl").getBytes(

      "ISO8859_1"), "GBK");

    if (httpurl.lastIndexOf("?show") == -1) {

     break exit;

    } else

     localfile = "/squid2/"

       + httpurl.substring(7, httpurl.length() - 5);

    try {

     start = Integer.parseInt(request.getParameter("start"));

    } catch (Exception e0) {

    }

    try {

     end = Integer.parseInt(request.getParameter("end"));

    } catch (Exception e0) {

    }

    response.reset();

    response.setContentType("video/x-flv");

    

    raf = new RandomAccessFile(localfile, "r");

    int length = (int) raf.length();

    if (end == 0)

     length = length - start;

    else

     length = end - start;

    // 当不是从头读起时,Add Flv header!

    byte[] data = new byte[] { 'F', 'L', 'V', 1, 1, 0, 0, 0, 9, 0,

      0, 0, 9 };

    if (start > 0) {

     response.setContentLength(length + 13);

     response.getOutputStream().write(data);

    } else

     response.setContentLength(length);

    data = new byte[1024];

    raf.skipBytes(start);

    for (int j = 0; j < length / 1024; j++) {

     raf.read(data);

     response.getOutputStream().write(data);

    }

    data = new byte[length % 1024];

    raf.read(data);

    response.getOutputStream().write(data);

    response.getOutputStream().flush();

   } catch (FileNotFoundException e1) {

    try {

     // 资源文件不存在,跳转到源站。

     response.sendRedirect("http://src_" + httpurl.substring(7)

       + "?show&start=" + start + "&end=" + end);

    } catch (Exception e2) {

    }

   } catch (Exception e3) {

   } finally {

    try {

     raf.close();

    } catch (Exception e4) {

    }

   }

  } else {

   try {

    // 盗链URL跳转

    response.sendRedirect("http://0.0.0.0");

   } catch (Exception e5) {

   }

  }

  this.destroy();

 }

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多