参考了很多大牛的文章,终于成功了,先感谢以下大牛,然后从前辈身上获利很多,我也应该把自己成功的经验奉献出来
ffmpeg参考:http://www.cnblogs.com/findingsea/archive/2013/03/14/2959634.html
mencoder参考:https://my.oschina.net/u/866216/blog/495107
视频上传到项目根目录,然后读取URL播放:http://www.cnblogs.com/dennisit/archive/2013/02/16/2913287.html#!comments
总结思路:
1. 上传commons-fileupload jar包
2. ckplay支持的视频格式flv,mp4,详细见官网吧
3. avi wmv等格式的用ffmpeg转码成flv
4.rmvb高清格式的用 mencoder视频转码成avi,然后在转码成flv
5. 高清格式的500M的视频转了20分钟不到,放在上传里面并不好,效果也不清楚模糊的
闲话少谈,上干货
项目结构:
springmvc配置我就不写了,网上有用的资料很多
1. 视频上传控制层
2. jsp页面
<div class="panel panel-default">
<div class="panel-body">
<div class="panel-heading" align="center"><h1 class="sub-header h3">文件上传</h1></div>
<hr>
<form class="form-horizontal" id="upload" method="post" action="${contextPath}/file/upload" enctype="multipart/form-data">
<div class="form-group" align="center">
<div class="col-md-4 col-sm-4 col-xs-4 col-lg-4">
文件上传 <input type="file" class="form-control" name="file" id="file">
<br>
<input type="submit" value="上传">
</div>
</div>
</form>
</div>
</div>
3. 控制层处理file
@RequestMapping(value = "/upload")
@ResponseBody
public ModelAndView upload(@RequestParam(value = "file", required = false) MultipartFile multipartFile, HttpServletRequest request, ModelMap map ) {
String message = "";
FileEntity entity = new FileEntity();
FileUploadTool fileUploadTool = new FileUploadTool();
//JSONObject getObj = new JSONObject();
try {
entity = fileUploadTool.createFile(multipartFile, request);
if(entity != null){
service.saveFile(entity);
message = "上传成功";
map.put("entity", entity);
map.put("result", message);
}else{
message = "上传失败";
map.put("result", message);
}
} catch (Exception e) {
e.printStackTrace();
}
return new ModelAndView("result", map);
}
4. 文件上传工具类
public class FileUploadTool {
TransfMediaTool transfMediaTool = new TransfMediaTool();
// 文件最大500M
private static long upload_maxsize = 800 * 1024 * 1024;
// 文件允许格式
private static String[] allowFiles = { ".rar", ".doc", ".docx", ".zip", ".pdf", ".txt", ".swf", ".xlsx", ".gif",
".png", ".jpg", ".jpeg", ".bmp", ".xls", ".mp4", ".flv", ".ppt", ".avi", ".mpg", ".wmv", ".3gp", ".mov",
".asf", ".asx", ".vob", ".wmv9", ".rm", ".rmvb" };
// 允许转码的视频格式(ffmpeg)
private static String[] allowFLV = { ".avi", ".mpg", ".wmv", ".3gp", ".mov", ".asf", ".asx", ".vob" };
// 允许的视频转码格式(mencoder)
private static String[] allowAVI = { ".wmv9", ".rm", ".rmvb" };
public FileEntity createFile(MultipartFile multipartFile, HttpServletRequest request) {
FileEntity entity = new FileEntity();
boolean bflag = false;
String fileName = multipartFile.getOriginalFilename().toString();
// 判断文件不为空
if (multipartFile.getSize() != 0 && !multipartFile.isEmpty()) {
bflag = true;
// 判断文件大小
if (multipartFile.getSize() <= upload_maxsize) {
bflag = true;
// 文件类型判断
if (this.checkFileType(fileName)) {
bflag = true;
} else {
bflag = fals