gpt4 book ai didi

grails - 上传MultipartFile时未捕获SizeLimitExceededException

转载 作者:行者123 更新时间:2023-12-02 14:48:29 26 4
gpt4 key购买 nike

上传大文件时,我想捕获异常SizeLimitExceededException。
但是,我在 Controller 中创建了一个方法来捕获此异常,但他返回:

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)
}

我的 Controller :
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)
}
}
}
}

谢谢您的帮助!

最佳答案

您没有提到grails版本。我假设它的3+

在application.yml中,您可以使用这些设置来设置要上传文件的最大大小。

grails:
controllers:
upload:
maxFileSize: 20000000
maxRequestSize: 20000000

本示例将请求和文件大小都设置为20MB。您可以根据需要进行调整。

关于grails - 上传MultipartFile时未捕获SizeLimitExceededException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52345945/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com