SSM上传,下载,在线播放视频

SSM上传,下载,在线播放视频

前言

基于ckplayer插件的视频在线播放,首先需要下载ckplayer插件。引入你的javaweb项目。

核心代码

    上传的Jsp代码
<form  method="POST"  enctype="multipart/form-data" action="${ctx}/traindata/add">
  <div >
      <label for="username">
           <span class="x-red">*</span>上传视频
      </label>
      <div>
           <input type="file" id="file" name="file"  >
      </div>
      </div>
   <div >
      <label for="L_repass" ></label>
          <input type="submit" value=" 提交"  lay-filter="add" lay-submit=""/>
    </div>
</form>
    下载的Jsp代码
<td align="center"  width="40px;"><a href="#" id="down_${dept.id }">
		<img width="20" height="20" title="下载" src="${ctx }/public/images/downLoad.png"/></a>
 </td>
 
<script type="text/javascript">
    $(function(){
          
   
    	/** 下载文档功能 */
    	$("a[id^=down_]").click(function(){
          
   
    		/** 得到需要下载的文档的id */
    		var id = this.id.replace("down_","");
    		/** 下载该文档 */
    		window.location = "${ctx}/traindata/downLoad?id="+id;
    	})
    })
    </script>
    播放Jsp代码
<script type="text/javascript">
	var flashvars={
          
   
		p:0,
		e:1,
		i:${ctx }/upload/suoluetu.png
		};
	var video=[${ctx }/upload/${requestScope.filename}->video/mp4];
	var support=[all];
	CKobject.embedHTML5(a1,ckplayer_a1,600,400,video,flashvars,support);
</script>
    上传 Controller代码展示
@RequestMapping(value="/traindata/add",method=RequestMethod.POST)
		 public ModelAndView add(ModelAndView mv,@ModelAttribute TrainData trainData ,Integer id,HttpSession session
				,HttpServletRequest request)
				 throws Exception{
          
   
				String path = session.getServletContext().getRealPath("/WEB-INF/upload");
				String filename = trainData.getFile().getOriginalFilename();
				
				 File tempFile = new File(path+"/"+filename);
				 tempFile.createNewFile();  
				 trainData.getFile().transferTo(tempFile); 
				 
				 trainData.setFilename(filename);
				 this.genericAssociation(user_id,trainData);
				 ahualyservice.insert_TrainDataInfo(trainData);
			   mv.setViewName("redirect:/traindata/list");
			return mv;
		}
    下载Controller代码展示
@RequestMapping(value="/traindata/downLoad")
		 public ResponseEntity<byte[]>  downLoad(Integer id,HttpSession session) throws Exception{
          
   
			// 根据id查询视频
			TrainData target = ahualyservice.get_TrainDataInfo(id);
			String fileName = target.getFilename();
		
			String path = session.getServletContext().getRealPath(  "/WEB-INF/upload");
			
			// 获得要下载文件的File对象
			File file = new File(path+"/"+ fileName);
			// 创建springframework的HttpHeaders对象
		    HttpHeaders headers = new HttpHeaders();  
	        // 下载显示的文件名,解决中文名称乱码问题  
	       String downloadFielName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");
	        // 通知浏览器以attachment(下载方式)打开图片
	        headers.setContentDispositionFormData("attachment", downloadFielName); 
	        // application/octet-stream : 二进制流数据(最常见的文件下载)。
	        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
	        // 201 HttpStatus.CREATED
	        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED); 
		}
    播放Controller代码
@RequestMapping(value="/traindata/play",method=RequestMethod.GET)
		 public String play(String filename,Model model){
          
   
       //根据自己的需求写,我这里是通过获取视频名称,所以做了字符串的处理
			filename=filename.substring(0,filename.length()-4);
			model.addAttribute("filename", filename);
			return "traindata/play";
		}

效果展示

播放效果

源码留言获取!!!

经验分享 程序员 微信小程序 职场和发展