我在做什么,发生了什么?
我正在尝试上传grails中的文件并将其下载。完成后,我仍然遇到问题,当文件大小时。这是一个例外:
Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException:
the request was rejected because its size (3553808) exceeds the configured maximum (128000)
我尝试的内容和结果:
我之前在this question找到了这个问题,答案是放置一些配置变量:
grails:
controllers:
upload:
maxFileSize: (10 * 1024 * 1024)
maxRequestSize: (10 * 1024 * 1024)
但仍然遇到同样的错误。我还尝试将一些依赖项添加为said here。或关闭IDE并重建。没有什么可以解决的。
有人面对这个问题并且可以解决它吗?
答案 0 :(得分:0)
以下为grails版本3.2及以上版本的正常工作
我们将允许上传25MB文件。
25 * 1024 * 1024 = 26.214.400字节
我的/grails-app/conf/application.yml
grails:
controllers:
upload:
maxFileSize: 26214400
maxRequestSize: 26214400
我的my.gsp
档案
<g:form action="saveImage" enctype="multipart/form-data" method="POST">
<input type="file" placeholder="Select file" name="file">
<g:submitButton value="Upload File"/>
</g:form>
我的controller
行动
def saveImage(){
def fileName = ''
def uploadedFile = request.getFile('file')
if (uploadedFile)
if (!uploadedFile.empty) {
try {
int dot = uploadedFile.originalFilename.lastIndexOf('.');
def fileExt = uploadedFile.originalFilename.substring(dot + 1);
fileName = ("myFile." + fileExt).toString()
def basePath = ''
if (Environment.current == Environment.PRODUCTION) {
basePath ='/var/local/prj/uploads/' // this works with production and tested on Ubuntu OS
} else {
basePath = grailsApplication.mainContext.servletContext.getRealPath('/uploads/') // this will take your project directory path Project\src\main\webapp\uploads folder
}
uploadedFile.transferTo(new File(basePath + fileName))
} catch (Exception e) {
e.printStackTrace()
}
}
// Your redirect code here
}
下载文件操作
def file = new File("Your file path")
if (file.exists()) {
response.setContentType("application/octet-stream") // or or image/JPEG or text/xml or whatever type the file is
response.setHeader("Content-disposition", "attachment;filename=\"${file.name}\"")
response.outputStream << file.bytes
} else
render "File does not exist!"
希望这有助于你
答案 1 :(得分:0)
问题在于分配cofig变量。我没有操作员分配它们:
maxFileSize: 10485760
maxRequestSize: 10485760
而不是:
maxFileSize: (10 * 1024 * 1024)
maxRequestSize: (10 * 1024 * 1024)
这就是我解决问题的方法。