离线下线与云点播

2017-08-22  本文已影响363人  lemonTreeTop

开了一台限流不限速的服务器,折腾一下离线下载和云点播

实现思路:

服务器安装 Aria2 ,启动命令:aria2c --conf-path=/var/www/a.conf -D(D表示后台运行)

安装httpd服务,下载webui文件https://github.com/ziahamza/webui-aria2,用来管理aria2

动手写jsp页面实现云点播,主要功能

index.jsp

<%@ page import="java.io.File" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title>云点播</title>
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="css/bootstrap.css" type="text/css" rel="stylesheet">
    <script type="application/javascript" src="js/jquery-2.2.3.min.js"></script>
    <script type="application/javascript" src="js/bootstrap.min.js"></script>
  </head>
  <body>
<%
    List<String> filenames=new ArrayList<>();
    String webpath=request.getSession().getServletContext().getRealPath("/");//项目文件夹
    String path=webpath+"/videos";//存放视频文件夹
    //System.out.println(path);

    //遍历文件夹的所有文件
    File file=new File(path);
    File[] fa=file.listFiles();

  for (int i=0;i<fa.length;i++){
        File fs=fa[i];
         if (fs.isDirectory())
         {
             System.out.println(fs.getName() + " [目录]");

         }

         else
         {
             filenames.add(fs.getName());
             System.out.println(fs.getName());

         }

  }

    System.out.println(filenames.size());
    pageContext.setAttribute("filelist",filenames);
%>

<div class="container">
  <div class="row clearfix">
    <div class="col-md-2 column">
    </div>
    <div class="col-md-8 column">
        <br><br>
      <div class="panel panel-info">
        <div class="panel-heading">
          <h3 class="panel-title" style="display: inline">
            简易云点播
          </h3>
            <a href="http://localhost/webui/" target="_blank" class="btn btn-info
             btn-sm" style="position: relative;left: 10%">离线下载</a>

        </div>

        <ul class="list-group">
            <c:forEach var="li" items="${filelist}">
              <li class="list-group-item">

            <div style="display: inline;font-size: 16px;"><c:out value="${li}"></c:out></div>
            <div style="display: inline;position:absolute;bottom: 5px;right: 0" >
              <a href="play.jsp?name=${li}" class="btn btn-success">播放</a>
              <a href="delete.jsp?name=${li}" class="btn btn-warning">删除</a>
            </div>
          </li>
          </c:forEach>
        </ul>
      </div>
    </div>
    <div class="col-md-2 column">
    </div>
  </div>
</div>
  </body>
</html>

play.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>play</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="css/bootstrap.css" type="text/css" rel="stylesheet">
</head>
<body>
<%
    String filename=request.getParameter("name");
    pageContext.setAttribute("name",filename);
%>
<center>
    <h4>${name}</h4>
<video controls="controls" autoplay="autoplay" width="100%" height="60%">
    <source src="videos/${name}" type="video/mp4" />
</video>
</center>
</body>
</html>

delete.jsp

<%@ page import="java.io.File" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>删除视频</title>
</head>
<body>
<center>
<%! public static boolean delete(String fileName) {
    File file = new File(fileName);
    if (!file.exists()) {
        System.out.println("删除文件失败:" + fileName + "不存在!");
        return false;
    } else {
            return file.delete();
    }
}

%>
    <%
        String filename=request.getParameter("name");
        String webpath=request.getSession().getServletContext().getRealPath("/");
        String path=webpath+"/videos";

        if (delete(path+filename)){

            out.print("删除成功");
        }else {
            out.print("删除失败");

        }
    %>
<br>
<a href="index.jsp">返回</a>
</center>
</body>
</html>

部署到服务器,投入使用


1.png 2.png
上一篇下一篇

猜你喜欢

热点阅读