依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
Service
private String filePath="D:/xxx/xxx";
public String upLoadFile(MultipartFile upload){
File file=new File(filePath);
if(!file.exists()){
file.mkdirs();
}
String originalFileName = upload.getOriginalFilename();
String newFileName = UUID.randomUUID()+originalFileName;
String newFilePath=filePath+newFileName;
try {
upload.transferTo(new File(newFilePath));
return newFileName;
}catch (IllegalStateException e ) {
}catch(IOException e1){
}
}
xml配置
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760"></property>
<property name="defaultEncoding" value="utf-8"></property>
</bean>