标签:
springMVC.xml 配置
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="uft-8" /> <!-- 默认编码 (ISO-8859-1) --> <property name="maxInMemorySize" value="10240" /> <!-- 最大内存大小 (10240)--> <property name="uploadTempDir" value="/upload/" /> <!-- 上传后的目录名 (WebUtils#TEMP_DIR_CONTEXT_ATTRIBUTE) --> <property name="maxUploadSize" value="-1" /> <!-- 最大文件大小,-1为无限止(-1) --> </bean>
jsp代码
<form action="spring/upload2" enctype="multipart/form-data" method="post"> 上传文件1:<input name="file" type="file"/><br/> 上传文件2:<input name="file" type="file"/><br/> <input type="submit" value="提交"> </form>
后台代码
@RequestMapping(value = "/upload2") public String filesUpload(@RequestParam("file") MultipartFile[] files, HttpServletRequest request) throws IOException { String message = ""; try { for (int i = 0; i < files.length; i++) { if (!files[i].isEmpty()) { // 开始上传时间 int startTime = (int) System.currentTimeMillis(); // 拿到文件名称 String fileName = files[i].getOriginalFilename(); // 上传路径 String path = "G:/" + fileName; System.out.println("文件名:" + fileName); // 上传 files[i].transferTo(new File(path)); message = "上传成功"; request.getSession().setAttribute("message", message); int endTime = (int) System.currentTimeMillis(); // 计算上传时间 System.out.println(endTime - startTime + "ms"); } message = "上传成功"; request.getSession().setAttribute("message", message); } } catch (FileNotFoundException e) { e.printStackTrace(); } return "forward:/WEB-INF/jsp/FileUpload/result.jsp"; }
标签:
原文地址:http://www.cnblogs.com/angto64/p/5153747.html