接口
MutipartResolver
实现类
CommonsMutipartResolver or StandardServletMutipartResolver
步骤
导入jar包
commons-io-2.6.jar commons-fileupload-1.3.3.jar springmvc-servlet.xml
#id必须严格按照multipartResolver编写,否则MVC会加载不了
< bean id = " multipartResolver" class = " org.springframework.web.multipart.commons.CommonsMultipartResolver" >
< property name = " maxUploadSize" value = " 5000000" > </ property>
< property name = " defaultEncoding" value = " UTF-8" > </ property>
</ bean>
View页面
< form id = " userForm" name = " userForm" method = " post" enctype = " multipart/form-data" action = " ${pageContext.request.contextPath }/user/useraddsave.html" >
< div>
< label for = " idPicPath" > 用户头像:</ label>
< input type = " file" name = " idPicPath" id = " idPicPath" />
</ div>
</ form>
Controller
@RequestMapping ( value= "/useraddsave.html" , method= RequestMethod. POST)
public String addUserSave ( User user, HttpSession session, HttpServletRequest request, @RequestParam ( value= "a_idPicPath" , required= false ) MultipartFile mFile) {
String idPicPath = null;
if ( ! mFile. isEmpty ( ) ) {
String path = request. getSession ( ) . getServletContext ( ) . getRealPath ( "statics" + File. separator) ;
String oldFileName = mFile. getOriginalFilename ( ) ;
String surfix = FilenameUtils. getExtension ( oldFileName) ;
int fileSize = 500000 ;
if ( mFile. getSize ( ) > fileSize) {
request. setAttribute ( "uploadFileError" , "uploadFile exceeds the MaxSize" ) ;
return "useradd" ;
} else if ( surfix. equalsIgnoreCase ( "jpg" ) || surfix. equalsIgnoreCase ( "png" ) ) {
String fileName = System. currentTimeMillis ( ) + RandomUtils. nextInt ( 1000000 ) + "_Personal.jpg" ;
File targetFile = new File ( path, fileName) ;
if ( ! targetFile. exists ( ) ) {
targetFile. mkdirs ( ) ;
}
try {
mFile. transferTo ( targetFile) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
request. setAttribute ( "uploadFileError" , "uploadFileError" ) ;
return "useradd" ;
}
idPicPath= path+ fileName;
} else {
request. setAttribute ( "surfixError" , "surfix does not match the rules" ) ;
return "useradd" ;
}
}
user. setCreatedBy ( ( ( User) session. getAttribute ( Constants. USER_SESSION) ) . getId ( ) ) ;
user. setCreationDate ( new Date ( ) ) ;
user. setIdPicPath ( idPicPath) ;
if ( userService. add ( user) ) {
return "redirect:/user/userlist.html" ;
}
return "useradd" ;
}
多文件上传
数组入参
@RequestMapping ( value= "/useraddsave.html" , method= RequestMethod. POST)
public String addUserSave ( User user, HttpSession session, HttpServletRequest request, @RequestParam ( value= "attachs" , required= false ) MultipartFile[ ] mFile) {
String idPicPath = null;
String errorinfo = null;
String workPicPath= null;
String category = null;
for ( int i= 0 ; i< mFile. length; i++ ) {
if ( i== 0 ) {
errorinfo= "uploadFileError" ;
category= "_Personal." ;
} else if ( i== 1 ) {
errorinfo= "uploadWpError" ;
category= "_Work." ;
}
if ( ! mFile[ i] . isEmpty ( ) ) {
String path = request. getSession ( ) . getServletContext ( ) . getRealPath ( "statics" + File. separator) ;
String oldFileName = mFile[ i] . getOriginalFilename ( ) ;
String surfix = FilenameUtils. getExtension ( oldFileName) ;
int fileSize = 500000 ;
if ( mFile[ i] . getSize ( ) > fileSize) {
request. setAttribute ( errorinfo, "uploadFile exceeds the MaxSize" ) ;
return "useradd" ;
} else if ( surfix. equalsIgnoreCase ( "jpg" ) || surfix. equalsIgnoreCase ( "png" ) ) {
String fileName = System. currentTimeMillis ( ) + RandomUtils. nextInt ( 1000000 ) + category+ surfix;
File targetFile = new File ( path, fileName) ;
if ( ! targetFile. exists ( ) ) {
targetFile. mkdirs ( ) ;
}
try {
mFile[ i] . transferTo ( targetFile) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
request. setAttribute ( errorinfo, "uploadFileError" ) ;
return "useradd" ;
}
if ( i== 0 ) {
idPicPath= path+ fileName;
} else if ( i== 1 ) {
workPicPath= path+ fileName;
}
} else {
request. setAttribute ( errorinfo, "surfix does not match the rules" ) ;
return "useradd" ;
}
}
}
user. setCreatedBy ( ( ( User) session. getAttribute ( Constants. USER_SESSION) ) . getId ( ) ) ;
user. setCreationDate ( new Date ( ) ) ;
logger. info ( idPicPath) ;
logger. info ( workPicPath) ;
user. setIdPicPath ( idPicPath) ;
user. setWorkPicPath ( workPicPath) ;
if ( userService. add ( user) ) {
return "redirect:/user/userlist.html" ;
}
return "useradd" ;
}
单独入参
@RequestMapping ( value= "/useraddsave.html" , method= RequestMethod. POST)
public String addUserSave ( User user, HttpSession session, HttpServletRequest request, @RequestParam ( value= "attachs" , required= false ) MultipartFile mFile, @RequestParam ( value= "attachs" , required= false ) MultipartFile mFile2) {
}