springboot出現(xiàn)DataBufferLimitException: Exceeded limit on max bytes to buffer:262144問題及解決
問題場景
spring boot 版本:2.2.2
應(yīng)用網(wǎng)關(guān):shenyu網(wǎng)關(guān)(soul網(wǎng)關(guān))
錯(cuò)誤描述
在一次項(xiàng)目中,我在上傳文件時(shí)被網(wǎng)關(guān)攔截出現(xiàn)了這個(gè)問題:
DataBufferLimitException: Exceeded limit on max bytes to buffer :262144
發(fā)現(xiàn)只能上傳256KB以下的文件,但現(xiàn)在文件隨便就是10M或者更大。
具體錯(cuò)誤如下:
org.springframework.core.io.buffer.DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144
at org.springframework.core.io.buffer.LimitedDataBufferList.raiseLimitException(LimitedDataBufferList.java:101)
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Stack trace:
at org.springframework.core.io.buffer.LimitedDataBufferList.raiseLimitException(LimitedDataBufferList.java:101)
at org.springframework.core.io.buffer.LimitedDataBufferList.updateCount(LimitedDataBufferList.java:94)
at
解決方案
方案一(對我不生效)
一般來說直接設(shè)置 max-in-memory-size 就可以生效
spring:
codec:
max-in-memory-size: 100MB但是以上代碼在spring2.x.x版本中不生效,官方說解決了,估計(jì)是在后面版本解決了,但是項(xiàng)目都上生產(chǎn)了肯定不可能現(xiàn)在來換版本,所以只有另想辦法,在他啟動(dòng)中debug發(fā)現(xiàn)在初始化代碼時(shí)已經(jīng)設(shè)置為262144了:

方案二(有效)
于是繼續(xù)往上發(fā)現(xiàn)在 org.springframework.core.codec.AbstractDataBufferDecoder中進(jìn)行初始化的,于是誕生了寫一個(gè)同名類來給他初始化,利用Spring中同名Bean相互覆蓋的特性,在我們注入了同名bean就會(huì)優(yōu)先使用我們的類。
層級目錄如下:

我們要保證這個(gè)類的包名和spring的名稱一模一樣,具體代碼不變,只需修改maxInMemorySize 大小,如下所示:
package org.springframework.core.codec;
import java.util.Map;
import org.reactivestreams.Publisher;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.MimeType;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* @Auther: whhh
* @Date: 2021/4/7 11:08
* @Description: 圖片上傳重寫緩存區(qū)大小,解決上傳圖片緩存不夠問題
*/
public abstract class AbstractDataBufferDecoder<T> extends AbstractDecoder<T> {
//圖片上傳重寫緩存區(qū)大小,解決上傳圖片緩存不夠問題
private int maxInMemorySize = 1024*1024*100;
protected AbstractDataBufferDecoder(MimeType... supportedMimeTypes) {
super(supportedMimeTypes);
}
public void setMaxInMemorySize(int byteCount) {
this.maxInMemorySize = byteCount;
}
public int getMaxInMemorySize() {
return this.maxInMemorySize;
}
public Flux<T> decode(Publisher<DataBuffer> input, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
return Flux.from(input).map((buffer) -> {
return this.decodeDataBuffer(buffer, elementType, mimeType, hints);
});
}
public Mono<T> decodeToMono(Publisher<DataBuffer> input, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
return DataBufferUtils.join(input, this.maxInMemorySize).map((buffer) -> {
return this.decodeDataBuffer(buffer, elementType, mimeType, hints);
});
}
/** @deprecated */
@Deprecated
@Nullable
protected T decodeDataBuffer(DataBuffer buffer, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
return this.decode(buffer, elementType, mimeType, hints);
}
}
重點(diǎn)就是這里,他會(huì)去讀取這個(gè)配置:

到這里我們的問題就解決了,上傳文件就成功了。

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- springboot項(xiàng)目打包的可執(zhí)行jar運(yùn)行報(bào)錯(cuò)問題及解決
- springboot啟動(dòng)報(bào)錯(cuò)Failed to load class [javax.servlet.Filter]的解決
- SpringBoot啟動(dòng)報(bào)錯(cuò):Update your application‘s configuration服務(wù)終止的解決
- IDEA運(yùn)行SpringBoot測試報(bào)錯(cuò)“命令行過長”的解決方法
- SpringBoot啟動(dòng)報(bào)錯(cuò)找不到或無法加載主類的三種解決方案
- springboot項(xiàng)目用maven插件打包時(shí)報(bào)錯(cuò)的解決方法
相關(guān)文章
java?ResourceBundle讀取properties文件方式
這篇文章主要介紹了java?ResourceBundle讀取properties文件方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Mybatis中的@Param及動(dòng)態(tài)SQL詳解
這篇文章主要介紹了Mybatis中的@Param及動(dòng)態(tài)SQL詳解,@Param是MyBatis所提供的作為Dao層的注解,作用是用于傳遞參數(shù),從而可以與SQL中的的字段名相對應(yīng),需要的朋友可以參考下2023-10-10
詳解MyBatis的動(dòng)態(tài)SQL實(shí)現(xiàn)原理
MyBatis提供了強(qiáng)大的動(dòng)態(tài)SQL語句生成功能,以應(yīng)對復(fù)雜的業(yè)務(wù)場景,本篇文章將結(jié)合MyBatis解析SQL語句的過程對MyBatis中對<if>,<where>,<foreach>等動(dòng)態(tài)SQL標(biāo)簽的支持進(jìn)行分析,需要的朋友可以參考下2023-07-07
為什么wait和notify必須放在synchronized中使用
這篇文章主要介紹了為什么wait和notify必須放在synchronized中使用,文章圍繞主題的相關(guān)問題展開詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考以參考一下2022-05-05
java基本教程之java線程等待與java喚醒線程 java多線程教程
這篇文章主要介紹了對線程等待/喚醒方法,文中使用了多個(gè)示例,大家參考使用吧2014-01-01

