选中多文件同时上传(利用input元素的multiple属性)
2016-05-09 10:45
477 查看
网页:
spring中配置:
java代码:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="upload.do" method="post" enctype="multipart/form-data"> <p>一个选择上传的文件</p> <input type="text" name="reck" value=""/><br/> <!-- 可以选取一张或者多种图片上传 --> <input type="file" name="files" multiple/><br/> <!-- 每个input元素只能选中一个文件 --> <!-- <input type="file" name="files" /><br/> <input type="file" name="files" /><br/> <input type="file" name="files" /><br/> --> <input type="submit" value="Submit"/> </form> </body> </html>
spring中配置:
<!-- ========文件上传======= --> <!--200*1024*1024即200M resolveLazily属性启用是为了推迟文件解析,以便捕获文件大小异常 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="209715200" /> <property name="defaultEncoding" value="UTF-8" /> <property name="resolveLazily" value="true" /> </bean>
java代码:
package org.wxy.demo.action; import java.io.File; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MaxUploadSizeExceededException; import org.springframework.web.multipart.MultipartFile; /** * * <一句话功能简述><p> * MultipartFile类常用的一些方法: * String getContentType() //获取文件MIME类型 * InputStream getInputStream() //后去文件流 * String getName() //获取表单中文件组件的名字 * String getOriginalFilename() //获取上传文件的原名 * long getSize() //获取文件的字节大小,单位byte * boolean isEmpty() //是否为空 * void transferTo(File dest) //保存到一个目标文件中。 * * @author wangxy * @param <E> * * @see [相关类/方法] * @since [产品/模块版本] */ @Controller public class FileUpDownCtrl<E> { /** * 上传文件 * @param file * @return */ @RequestMapping(value="/upload.do") public String fileUpload(@RequestParam("files") MultipartFile[] files,HttpServletRequest request) { String rect = (String) request.getAttribute("rect"); System.out.println(rect); //判断file数组不能为空并且长度大于0 if(files!=null && files.length>0){ //循环获取file数组中得文件 for(int i = 0;i<files.length;i++){ MultipartFile file = files[i]; try { //获取存取路径 String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/" + file.getOriginalFilename(); // 转存文件 file.transferTo(new File(filePath)); } catch (IOException e) { e.printStackTrace(); } } // 重定向 return "redirect:/list.do"; } // 重定向 return "redirect:/fail.html"; } @RequestMapping(value="/list.do") @ResponseBody public String list(HttpServletRequest request,HttpServletResponse response) { String filePath = request.getSession().getServletContext().getRealPath("/") + "upload"; File[] files = new File(filePath).listFiles(); StringBuilder fileNames = new StringBuilder(); for(File f : files){ System.out.println(f.getName()); fileNames.append(f.getName()+"\n"); } return fileNames.toString(); } /** * 配置了 <property name="resolveLazily" value="true" /> * 才有效 * @author wangxy * * @param ex */ @ExceptionHandler public void doExcepiton(Exception ex){ if(ex instanceof MaxUploadSizeExceededException){ System.out.println("文件太大"); } } }
相关文章推荐
- swfupload 多文件上传实现代码
- php 动态多文件上传
- PHP文件上传之多文件上传的实现思路
- 实现ASP.NET多文件上传程序代码
- PHP实现的多文件上传类及用法示例
- PHP5+UTF8多文件上传类
- php多文件上传实现代码
- php jquery 多文件上传简单实例
- Codeigniter实现多文件上传并创建多个缩略图
- asp.net文件上传解决方案(图片上传、单文件上传、多文件上传、检查文件类型)
- asp.net批量多选文件上传解决方案
- Zend Framework实现多文件上传功能实例
- PHP 多文件上传的三种方法
- PHP实现的多文件上传类及用法示例
- Zend Framework实现多文件上传功能实例
- 文件上传
- php多文件上传-简单示例,适合初学参考。
- java struts2 多文件上传 加进度条
- 网易邮箱多文件上传机制初探
- MVC4中使用Jquery.Multifile.js和Jquery.form.js一次上传多个文件