多语言展示
当前在线:1863今日阅读:19今日分享:20

uploadify3.2.1+springmvc实现多文件上传

uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示,本文将之转换为java语言描述外加springmvc实现,我将在student demo中描述了该插件的使用
工具/原料

eclipse+springmvc环境

方法/步骤
1

在Jsp页面中编写Script代码实现上传事件的描述,必须要注意其中的路径问题,uploader(springmvc controller处理的位置),其他两个路径为uploadify文件的存放路径(这个可以到官网下载)

2

uploadify下载下来的文件的存放路径

3

在Jsp页面中编写页面的具体布置

4

实现页面如下

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

推荐信息