Я хочу поймать исключение SizeLimitExceededException при загрузке большого файла.Тем не менее, я создал метод в контроллере для перехвата этого исключения, но он возвращает:
Error 500: Internal Server Error
URI
/user/uploadProfileImage
Class
org.apache.tomcat.util.http.fileupload
.FileUploadBase$SizeLimitExceededException
Message
Could not parse multipart servlet request; nested exception is
java.lang.IllegalStateException:
org.apache.tomcat.util.http.fileupload
.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (310380) exceeds the configured maximum (128000)
Caused by
the request was rejected because its size (310380) exceeds the configured maximum (128000)
Метод перехвата исключения:
def handleSizeLimitExceededException(FileUploadBase.SizeLimitExceededException ex){
render(['error':true, message:ex.getMessage()] as JSON)
}
Мой контроллер:
class UserController {
...
def uploadProfileImage(ProfileImageCommand cmd) {
if (cmd.hasErrors()) {
//respond(cmd, model: [user: cmd], view: 'create')
response.status = 500
render(['error':true, message:cmd.getErrors()] as JSON)
}
String filename = uploadFileService.uploadFileInTemp(cmd, relativePathTmp)
Locale locale = request.locale
//flash.message = crudMessageService.message(CRUD.UPDATE, domainName(locale), pointOfInterest.id, locale)
render(['filename':filename, 'url': uploadFileService.getUrl(filename, relativePathTmp)] as JSON)
}
def handleSizeLimitExceededException(FileUploadBase.SizeLimitExceededException ex){
render(['error':true, message:ex.getMessage()] as JSON)
}
}
Мои данные:
class ProfileImageCommand implements Validateable {
MultipartFile imageFile
static constraints = {
imageFile validator: { val, obj ->
if ( val == null ) {
return false
}
if ( val.empty ) {
return false
}
['jpeg', 'jpg', 'png'].any { extension ->
val.originalFilename?.toLowerCase()?.endsWith(extension)
}
}
}
}
Спасибо за помощь!