Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

如何配置Spring Boot每个Controller控制不同的文件上传大小最大值?

我知道有一个配置项multipart.max-file-size可以控制文件上传大小,但这个值为一全局配置无法细化到每个Controller,而使用MultipartFile类的getSize()方法判断大小的话文件必然已经全部上传到服务器了,我需要一个类似multipart.max-file-size配置项的效果,如果文件超出指定大小后直接中断请求,并能在每一个Controller中进行不同值得设置,谢谢。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

提供一种解决思路
使用 multipart.max-file-size 配置最大的文件上传的大小=>然后在代码里获取上传文件的大小=>判断是否满足条件

伪代码 :

Boolean upload(String path,File file,Integer maxSize){
    Intege fileSize = file.size();
    if(fileSize<=maxSize){
        //上传...
        return true;       
    }else{
        return false;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...