1.applicationContext.xml
1 <bean id="multipartResolver" 2 class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 3 <property name="maxUploadSize" value="104857600" /> 4 <property name="maxInMemorySize" value="4096" /> 5 <property name="defaultEncoding" value="UTF-8"></property> 6 </bean>
2.uploadFile.jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 11 <form name="Form2" action="/tao-manager-web/springUpload" method="post" 12 enctype="multipart/form-data"> 13 <h1>使用spring mvc提供的类的方法上传文件</h1> 14 <input type="file" name="file"> <input type="submit" 15 value="upload" /> 16 </form> 17 </body> 18 </html>
3.UploadFileAction.java
1 package com.taozhiye.controller; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.OutputStream; 9 import java.util.Date; 10 import java.util.Iterator; 11 12 import javax.servlet.http.HttpServletRequest; 13 14 import org.springframework.stereotype.Controller; 15 import org.springframework.web.bind.annotation.RequestMapping; 16 import org.springframework.web.bind.annotation.RequestParam; 17 import org.springframework.web.bind.annotation.ResponseBody; 18 import org.springframework.web.multipart.MultipartFile; 19 import org.springframework.web.multipart.MultipartHttpServletRequest; 20 import org.springframework.web.multipart.commons.CommonsMultipartFile; 21 import org.springframework.web.multipart.commons.CommonsMultipartResolver; 22 23 @Controller 24 public class UploadFileAction { 25 26 27 @RequestMapping("uploadFile") 28 public String goFile(){ 29 return "uploadFile"; 30 } 31 32 /* 33 * 采用spring提供的上传文件的方法 34 */ 35 @RequestMapping("springUpload") 36 @ResponseBody 37 public String springUpload(HttpServletRequest request) 38 throws IllegalStateException, IOException { 39 long startTime = System.currentTimeMillis(); 40 // 将当前上下文初始化给 CommonsMutipartResolver (多部分解析器) 41 CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( 42 request.getSession().getServletContext()); 43 // 检查form中是否有enctype="multipart/form-data" 44 if (multipartResolver.isMultipart(request)) { 45 // 将request变成多部分request 46 MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; 47 // 获取multiRequest 中所有的文件名 48 Iterator iter = multiRequest.getFileNames(); 49 while (iter.hasNext()) { 50 // 一次遍历所有文件 51 MultipartFile file = multiRequest.getFile(iter.next() 52 .toString()); 53 if (file != null) { 54 String path = "E:/springUpload" 55 + file.getOriginalFilename(); 56 // 上传 57 file.transferTo(new File(path)); 58 } 59 60 } 61 62 } 63 long endTime = System.currentTimeMillis(); 64 System.out.println("方法的运行时间:" + String.valueOf(endTime - startTime) 65 + "ms"); 66 return "success"; 67 } 68 } 69