html
<form name="Form2" action="/SpringMVC006/fileUpload2" method="post" enctype="multipart/form-data">
<h1>采用multipart提供的file.transfer方法上传文件</h1>
<input type="file" name="file">
<input type="submit" value="upload"/>
</form>
配置springmvc.xml:
1
2
3
4
5
6
|
<!-- 多部分文件上传 --> <bean id= "multipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<property name= "maxUploadSize" value= "104857600" />
<property name= "maxInMemorySize" value= "4096" />
<property name= "defaultEncoding" value= "UTF-8" ></property>
</bean> |
使用方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/* * 采用file.Transto 来保存上传的文件
*/
@RequestMapping ( "fileUpload2" )
public String fileUpload2( @RequestParam ( "file" ) CommonsMultipartFile file) throws IOException {
long startTime=System.currentTimeMillis();
String path= "E:/" + new Date().getTime()+file.getOriginalFilename();
File newFile= new File(path);
//通过CommonsMultipartFile的方法直接写文件(注意这个时候)
file.transferTo(newFile);
return "/success" ;
}
|