1. project-servlet.xml
<!-- MultipartResolver for parsing file uploads, implementation for Commons FileUpload -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize">
<value>2000000</value>
</property>
</bean>
2. upload.html
<form action="" method="post" encType="multipart/form-data">
<input type="file" name="f">
<input type="submit">
</form> 3. Controller.java
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile multipartFile = multipartRequest.getFile("f");
private final String uploadDir= "uploadDir";
String filePathName = getServletContext().getRealPath(File.separator + uploadDir);
File filePath = new File(filePathName);
if (!filePath.exists()) { filePath.mkdirs(); }
String fileName = filePathName + File.separator + multipartFile.getOriginalFilename();
File file = new File(fileName);
multipartFile.transferTo(file);
参考:
1. spring's sample: imagedb
2. spring reference