spring boot 中出现DataBufferLimitException: Exceeded limit on max bytes to buffer :262144
目录
问题场景
错误描述
解决方案
方案一(对我不生效)
方案二(有效)
问题场景
spring boot 版本:2.2.2
应用网关:shenyu网关(soul网关)
错误描述
在一次项目中,我在上传文件时被网关拦截出现了这个问题:DataBufferLimitException: Exceeded limit on max bytes to buffer :262144,发现只能上传256KB以下的文件,但现在文件随便就是10M或者更大。
具体错误如下:
org.springframework.core.io.buffer.DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144at 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
解决方案
方案一(对我不生效)
一般来说直接设置 max-in-memory-size 就可以生效
spring:codec:max-in-memory-size: 100MB
但是以上代码在spring2.x.x版本中不生效,官方说解决了,估计是在后面版本解决了,但是项目都上生产了肯定不可能现在来换版本,所以只有另想办法,在他启动中debug发现在初始化代码时已经设置为262144了:
方案二(有效)
于是继续往上发现在 org.springframework.core.codec.AbstractDataBufferDecoder中进行初始化的,于是诞生了写一个同名类来给他初始化,利用Spring中同名Bean相互覆盖的特性,在我们注入了同名bean就会优先使用我们的类。
层级目录如下:
我们要保证这个类的包名和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: 图片上传重写缓存区大小,解决上传图片缓存不够问题*/
public abstract class AbstractDataBufferDecoder<T> extends AbstractDecoder<T> {//图片上传重写缓存区大小,解决上传图片缓存不够问题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@Nullableprotected T decodeDataBuffer(DataBuffer buffer, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {return this.decode(buffer, elementType, mimeType, hints);}
}
重点就是这里,他会去读取这个配置:
到这里我们的问题就解决了,上传文件就成功了。
spring boot 中出现DataBufferLimitException: Exceeded limit on max bytes to buffer :262144相关推荐
- Spring DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144
文章目录 Spring DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144 前言 问题分析 问题解决过程 ...
- spring cloud gateway [DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144]
测试时报错:DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144 大概意思:数据缓存限制异常:超过缓冲区的最 ...
- webflux webclient DataBufferLimitException: Exceeded limit on max bytes to buffer
问题 spring weblux(netty,版本2.3.6.RELEASE) webclient发送http请求出现org.springframework.core.io.buffer.DataBu ...
- Exceeded limit on max bytes to buffer : 262144
springboot版本 2.7.3 springcloud版本 2021.0.3 POST请求的body是1M多,请求网关提示报错. org.springframework.core.io.buff ...
- gateWay报错:Exceeded limit on max bytes to buffer : 262144
前端富文本框上传大图片,转成base64格式请求后端接口报错. 搜了很多资料,加spring: codec: max-in-memory-size:配置,写Filter都不生效. 之后看源码,原因是 ...
- 再谈Spring Boot中的乱码和编码问题
编码算不上一个大问题,即使你什么都不管,也有很大的可能你不会遇到任何问题,因为大部分框架都有默认的编码配置,有很多是UTF-8,那么遇到中文乱码的机会很低,所以很多人也忽视了. Spring系列产品大 ...
- 【spring boot2】第8篇:spring boot 中的 servlet 容器及如何使用war包部署
嵌入式 servlet 容器 在 spring boot 之前的web开发,我们都是把我们的应用部署到 Tomcat 等servelt容器,这些容器一般都会在我们的应用服务器上安装好环境,但是 spr ...
- Spring Boot 中使用 MongoDB 增删改查
本文快速入门,MongoDB 结合SpringBoot starter-data-mongodb 进行增删改查 1.什么是MongoDB ? MongoDB 是由C++语言编写的,是一个基于分布式文件 ...
- Spring Boot 中使用@Async实现异步调用,加速任务执行!
欢迎关注方志朋的博客,回复"666"获面试宝典 什么是"异步调用"?"异步调用"对应的是"同步调用",同步调用指程序按照 ...
最新文章
- easyUI与选择WebUI
- [原创]软件测试过程改进的内容和注意事项
- java之整数的分解可以理解为倒序输出
- P1016 旅行家的预算
- 感悟琐记:业绩=资源的n次方
- samba访问其他服务器文件权限设置
- php苹果推送消息,php推送消息到IOS
- 新手干货:Vue - 常用指令
- poj3274 找平衡数列(哈希加一点数学思维)
- Cloudera Manager Agent 的 Parcel 目录位于可用空间小于 5.0 吉字节 的文件系统上。 /opt/cloudera/parcels(可用:5.0 吉字节 (12.74%)
- 【长难句分析精讲】状语从句
- 大数据解决方案,案例分享
- 在 Github 制作在线简历 PDF
- 一、【VUE-CLI】Vue CLI 脚手架介绍及安装
- 【光线追踪】 流程分析与实现的路径跟踪渲染器
- 做短视频必须要知道的几个视频设置参数,爆款必备。
- C# 按Esc键关闭窗体
- 什么是MES生产管理和生产制造执行系统?有哪些系统模块组成?
- STM32L4的待机模式闹钟唤醒方法
- 法拉利虚拟学院2010 服务器,法拉利虚拟学院