javaSpringMVC上传文件(图片、文档、视频)及在线浏览

servlet上传文件:https://blog.csdn.net/baidu_35468322/article/details/88871675
上传(jsp):

类型:
标题:
简介:
附件类型:
附件:
状态: 可用     不可用
备注:
保存 取消

上传(controller):

 

 

        /**
	 * 添加
	 * @param file
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/saveCK")
	public ModelAndView saveCK(@RequestParam MultipartFile file,HttpServletRequest request) throws Exception{
		if(!Jurisdiction.buttonJurisdiction(ckUrl, "save")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd=this.getPageData();
		pd.put("CK_ID", this.get32UUID());
		pd.put("TP_ID", request.getParameter("tp_id"));
		pd.put("CK_TITLE", request.getParameter("ck_title"));
		pd.put("CK_BLURB", request.getParameter("ck_blurb"));
		pd.put("CK_ATTACH_TYPE", request.getParameter("ck_attach_type"));
		//图片、视频、文档的路径
		String url="";
		// 文件保存路径
		if("0".equals(pd.getString("CK_ATTACH_TYPE"))){
			//图片
			url = "/images/";
		}else if("1".equals(pd.getString("CK_ATTACH_TYPE"))){
			//视频
			url = "/video/";
		}else if("2".equals(pd.getString("CK_ATTACH_TYPE"))){
			//文档
			url = "/document/";
		}
		
		pd.put("CK_BELONG", request.getParameter("ck_belong"));	//0--案例知识     1--教学素材
		
		//获取当前登录人
		HttpSession session = getRequest().getSession(); 
		User user = (User)session.getAttribute(Const.SESSION_USER);
		//如果是学生则添加的时候为无     如果是老师   则显示老师name
		//通过用户Id获取角色Id   从而得出角色名称
		pd.put("ROLE_ID", user.getROLE_ID());
		if(roleService.findObjectById(pd).getString("ROLE_NAME").equals("学生")){
			pd.put("USER_ID", "");
			pd.put("AUDIT_TIME", "无");
			pd.put("AUDIT_STATE", "2");				//还要判断  如果是学生则需要审核     如果是老师   则不需要审核
		}else{
			pd.put("USER_ID", user.getUSER_ID());	
			pd.put("AUDIT_TIME", ContractController.getTime());
			pd.put("AUDIT_STATE", "0");				//还要判断  如果是学生则需要审核     如果是老师   则不需要审核
		}
		pd.put("CK_STATE", request.getParameter("ck_state"));			
		if("".equals(request.getParameter("remarks"))){
			pd.put("REMARKS", "无");
		}else{
			pd.put("REMARKS", request.getParameter("remarks"));
		}
		pd.put("CREATE_BY", user.getNAME());
		pd.put("CREATION_DATE", ContractController.getTime());
		pd.put("LAST_UPDATE_DATE", ContractController.getTime());
		pd.put("TEMP1", "");
		pd.put("TEMP2", "");
		pd.put("TEMP3", "");
		System.out.println("pd-->"+pd);
		//获取文件名
		String fileName = file.getOriginalFilename();
		//文件扩展名
		String extName = fileName.substring(fileName.lastIndexOf("."));
		String newName = this.get32UUID()+extName;
		System.out.println("file.getOriginalFilename()"+file.getOriginalFilename());
		pd.put("CK_ATTACH_PATH", "knowledge/"+url+newName);
		// 判断文件是否为空
		if (!file.isEmpty()) {
			try {
				String filePath = request.getSession().getServletContext().getRealPath("uploadFiles/knowledge")+url+newName;
				System.out.println("filePath-->"+filePath);
				// 转存文件
				file.transferTo(new File(filePath));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		caseKnowledgeService.saveCK(pd);
		mv.addObject("CK_BELONG", request.getParameter("ck_belong"));
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		//mv.setViewName("redirect:list.do?CK_BELONG="+request.getParameter("ck_belong"));
		return mv;
	}

删除:

 

 

	/**
	 * 删除
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteCK")
	public ModelAndView deleteCK(HttpServletRequest request) throws Exception{
		if(!Jurisdiction.buttonJurisdiction(ckUrl, "del")){return null;} //校验权限
		ModelAndView mv=this.getModelAndView();
		PageData pd=this.getPageData();
		PageData caseKnow = caseKnowledgeService.findById(pd);
		//System.out.println("**********"+request.getSession().getServletContext().getRealPath("uploadFiles/"+caseKnow.getString("CK_ATTACH_PATH")));
		File file = new File(request.getSession().getServletContext().getRealPath("uploadFiles/"+caseKnow.getString("CK_ATTACH_PATH")));
		if (!file.isDirectory()) {//如果文件没有被销毁,则删除
			file.delete();
		}
		caseKnowledgeService.deleteCK(pd);
		mv.addObject("msg","success");
		mv.addObject("CK_BELONG", pd.getString("CK_BELONG"));
		mv.setViewName("save_result");
		return mv;
	}

在线预览(controller):

 

 

	/**
	 * 查看附件
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/view")
	public ModelAndView view(HttpServletRequest request) throws Exception{
		if(!Jurisdiction.buttonJurisdiction(ckUrl, "cha")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		System.out.println("pd-->"+pd);
		pd=caseKnowledgeService.findById(pd);
		//判断路径是否存在
		File file = new File(request.getSession().getServletContext().getRealPath("uploadFiles/"+pd.getString("CK_ATTACH_PATH")));    
		if(!file.exists()) {  //如果不存在  
	    	pd.put("path", "hasnotPath");   
		}
			mv.addObject("pd", pd);
		mv.setViewName("knowledge/caseKnowledge/ck_view");
		return mv;
	}

在线预览(jsp):

 

 

    
文件找不到了!

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(java)