1 出发点及效果图
手机存储空间太小,放不下几十级的视频,网络看又太卡。这个小项目帮助大家在局域网中建立一个像youku一样的视频服务器,这样就可以在家里通过局域网来观看服务
器(计算机)上的视频了。(不要做羞羞的事情哟)
2 原理
主要是利用了html中的vedio标签,在tomcat服务器下播放指定文件夹中的mp4视频。
2.1 源码展示
2.1.1 获得视频列表的工具类
import java.util.ArrayList;
public class file {
private static String path = "E:/apache-tomcat-7.0.62/apache-tomcat-7.0.62/webapps/vedio";
/**
*
* @param dir
* @throws Exception
*/
static void showAllFiles(File dir) throws Exception {
File[] fs = dir.listFiles();
for (int i = 0; i < fs.length; i++) {
System.out.println(fs[i].getName());
if (fs[i].isDirectory()) {
try {
showAllFiles(fs[i]);
} catch (Exception e) {
}
}
}
}
public static ArrayList<String> getallfiles() throws Exception {
File dir = new File(path);
File[] fs = dir.listFiles();
ArrayList<String> filelist = new ArrayList<String>();
for (int i = 0; i < fs.length; i++) {
System.out.println(fs[i].getName());
if (fs[i].isDirectory()) {
try {
showAllFiles(fs[i]);
} catch (Exception e) {
}
} else {
filelist.add(fs[i].getName());
}
}
return filelist;
}
public static void main(String[] args) {
File root = new File(path);
try {
showAllFiles(root);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.1.2 播放视频的jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
String vediopath = (String) request.getAttribute("vediopath");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'playmyvedio.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no,minimal-ui">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<video controls autobuffer> <source src="http://192.168.7.101:8080/vedio/<%=vediopath%>"
type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'></source> </video>
</body>
</html>
3 项目下载
http://download.youkuaiyun.com/detail/zhounanzhaode/8905437
4 使用
4.1 安装tomcat7.0以上版本
4.2 部署项目
4.3 在webapp目录下面建立vedio目录用来存放视频
5 局限
只能播mp4,其它的没有试过,有可能能播,但是mp4效果是最好的。推荐用狸窝全能视频转换器转成mp4。