日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring boot配置对上传文件的大小限制

發(fā)布時間:2024/7/23 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring boot配置对上传文件的大小限制 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

spring boot配置對上傳文件的大小限制

spring-boot 2.0之前

Spring Boot1.4版本后配置更改為(默認單個文件最大1Mb,單次請求文件總數(shù)大小最大10Mb):
spring.http.multipart.maxFileSize = 20MB
spring.http.multipart.maxRequestSize=20MB

spring-boot 2.0之后:

Spring Boot2.0之后的版本配置修改為(默認單個文件最大1Mb,單次請求文件總數(shù)大小最大10Mb):
spring.servlet.multipart.max-file-size = 20MB
spring.servlet.multipart.max-request-size=20MB

超過20Mb,就會報錯,如下:
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (25766453) exceeds the configured maximum (20971520)

還有其他一些配置,都是spring.http.改為spring.servlet.

1.啟動報錯:


APPLICATION FAILED TO START


Description:

Failed to bind properties under ‘spring.servlet.multipart.max-file-size’ to org.springframework.util.unit.DataSize:

Property: spring.servlet.multipart.max-file-size Value: 20Mb Origin: "spring.servlet.multipart.max-file-size" from property source "application" Reason: failed to convert java.lang.String to org.springframework.util.unit.DataSize

解決:

spring.http.multipart.maxFileSize = 20Mb 改為 spring.http.multipart.maxFileSize = 20MB
及Mb改為MB
或者,就寫spring.http.multipart.maxFileSize=20,不要加單位,也是對的,唯獨加Mb就會報錯

2.上傳報錯:

The temporary upload location [C:\Users(你的路徑).8080\work\Tomcat\localhost\ROOT] is not valid
我的spring-boot版本是2.1.1
配置臨時路徑:
spring.http.multipart.location= D:\aa
我的D盤也有這個aa的文件夾,但就是報錯

百度說要增加一個bean,然后手動設置臨時路徑,原文:https://www.snowruin.com/?p=1728
具體如下:

@Bean MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); factory.setLocation("D:\aa"); return factory.createMultipartConfig(); }

這樣應該可以,但我認為既然spring-boot給了配置項,就盡量不要重寫bean

我覺得應該是我配置spring.http.multipart.location=D:\aa 這個有問題了
試啊試,當我把spring.http.multipart.location=D:\aa 改成spring.servlet.multipart.location=D:\aa,竟然好了

總結

以上是生活随笔為你收集整理的spring boot配置对上传文件的大小限制的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。