我的项目目录中有每个用户的图像,例如 user1.jpeg、user2.jpeg 等。但是当我尝试更改用户图像时,它会出错。我不明白该怎么做。这是错误如下>>>
Cannot cast object 'org.apache.catalina.core.ApplicationHttpRequest@21740230' with class 'org.apache.catalina.core.ApplicationHttpRequest' to class 'org.springframework.web.multipart.MultipartHttpServletRequest'
这是我的更新操作>>>
def updateUser = {
String message = ""
MultipartHttpServletRequest mpr = (MultipartHttpServletRequest)request;
CommonsMultipartFile f = (CommonsMultipartFile) mpr.getFile("userPhoto")//getFile("userPhoto");
if(!f.empty) {
def user = User.get(1)
user.avatarType = f.getContentType()
if(user.save()){
def userId = user.id
String username = user.username
println(userId)
String fileName = username + "." + f.getContentType().substring(6)
new File( grailsApplication.config.images.location.toString() ).mkdirs()
f.transferTo( new File( grailsApplication.config.images.location.toString() + File.separatorChar + fileName) )
message = "Here is your updated Information >>> "
render(view: 'userInfo', model: [message: message],)
}else{
message = "Can not Update User !!!"
render(view: 'editUser', model:[message: message])
return;
}
}else {
flash.message = 'file cannot be empty'
}
}
有人可以帮我吗?我正在使用 grails 2.1.0。
编辑 ::
这是编辑用户的视图>>>
<div class="main">
<g:form controller="user" action="updateUser">
User Name : ${username} <br/>
Photo : <input type="file" name="userPhoto" /> <p></p>
<g:hiddenField name="userId" id="userId" value="${userId}"/>
<g:submitButton name="updateUser" value="Update" />
</g:form>