1、View层:file.jsp
<form method="post" action="/courses/doUpload" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit"/>
</form>
2、配置
<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>
3、Controller层
@RequestMapping(value="/doUpload", method=RequestMethod.POST)
public String doUploadFile(@RequestParam("file") MultipartFile file) throws IOException{
if(!file.isEmpty()){
log.debug("Process file: {}", file.getOriginalFilename());
FileUtils.copyInputStreamToFile(file.getInputStream(), new File("D:\\file\\", System.currentTimeMillis()+ file.getOriginalFilename()));
}
return "success";
}