uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示,本文将之转换为java语言描述外加springmvc实现,我将在student demo中描述了该插件的使用
工具/原料
方法/步骤
1在Jsp页面中编写Script代码实现上传事件的描述,必须要注意其中的路径问题,uploader(springmvc controller处理的位置),其他两个路径为uploadify文件的存放路径(这个可以到官网下载)
5在controller目录中创建类对jsp页面中的请求进行处理,由于代码比较多,所以截图分两部分,最后面给出具体的代码文字
6/** * 处理文件上传 * @param response * @param request * @return * @throws IOException */ @RequestMapping(value='/uploadFile',method=RequestMethod.POST) public String upload(HttpServletResponse response,HttpServletRequest request) throws IOException{ String responseStr='';MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; Map fileMap = multipartRequest.getFileMap(); //String savePath = this.getServletConfig().getServletContext().getRealPath(''); //savePath = savePath + '/uploads/'; // 文件保存路径 ctxPath本地路径 String ctxPath=request.getSession().getServletContext().getRealPath('/')+File.separator+'uploadFiles'; SimpleDateFormat sdf = new SimpleDateFormat('yyyyMM'); String ymd = sdf.format(new Date()); ctxPath += File.separator + ymd + File.separator; System.out.println('ctxpath='+ctxPath); // 创建文件夹 File file = new File(ctxPath); if (!file.exists()) { file.mkdirs(); } String fileName = null; for (Map.Entry entity : fileMap.entrySet()) { // 上传文件 MultipartFile mf = entity.getValue(); fileName = mf.getOriginalFilename();//获取原文件名 System.out.println('filename='+fileName); File uploadFile = new File(ctxPath + fileName); try { FileCopyUtils.copy(mf.getBytes(), uploadFile); responseStr='上传成功'; } catch (IOException e) { responseStr='上传失败'; e.printStackTrace(); } } return responseStr; }
注意事项
1需要自己修改一些路径问题,这方面自己要仔细点去了解
2经验贴排版的问题,附上的代码没有很好的缩进效果,这个请见谅
3百度云源代码:http://pan.baidu.com/s/1c0pVwkw