如何在Spring Boot中更改Multipart文件的最大大小?

时间:2018-10-10 13:34:41

标签: spring spring-boot

我尝试用邮递员上传一首歌曲,但出现类似该现场歌曲超出其最大允许大小1048576字节的错误。”

我已经尝试在application.properties文件中添加此配置,但是它不起作用:

spring.servlet.multipart.max-file-size = 10MB spring.servlet.multipart.max-request-size = 10MB

我精确到此操作适用于<1048576字节的文件

这是我的控制器:

@PostMapping("/songs")
    public ResponseEntity<?> insertSong( @RequestParam(value = "title") String title, @RequestParam(value = "song") MultipartFile file) throws IOException {

       Song song= new Song(title);

        songService.insertSong(song, file);

        return ResponseEntity.ok("song inserted");
    }

这是我的服务

@Service
public class SongServiceImpl implements SongService {

    @Autowired
    SongRepository songRepository;

    @Value("${dir.songs}")
    private  String songsFolderPath;

    @Override
    public void insertSong(Song song, MultipartFile file) throws IOException 
    {

        if(!(file.isEmpty())){
            song.setSongPath(file.getOriginalFilename());
            songRepository.save(song);

            if (!(file.isEmpty())){
                song.setSongPath(file.getOriginalFilename());
                file.transferTo(new 
           File(songsFolderPath+song.getSong_ID()));
            }
        }
    }

这是我的错误消息: {“ timestamp”:“ 2018-10-10T13:27:07.090 + 0000”,“ status”:500,“ error”:“内部服务器错误”,“ message”:“超出了最大上传大小;嵌套异常是java.lang.IllegalStateException:org.apache.tomcat.util.http.fileupload.FileUploadBase $ FileSizeLimitExceededException:现场歌曲超过了其最大允许大小1048576字节。“,”路径“:” /音乐服务/歌曲“}

我使用微服务,我的配置在gitLab上,是这个:

enter image description here

我像这样使用邮递员插入我的文件:

enter image description here

3 个答案:

答案 0 :(得分:6)

对于spring-boot版本2.x.x,请通过添加http来尝试以下代码。

spring.http.multipart.max-file-size=300MB
spring.http.multipart.max-request-size=300MB

引用set-maxfilesize

答案 1 :(得分:4)

更新了SpringBoot 2.1.2发行版

Web应用程序中的多部分尺寸取决于两个单独的设置

1)应用程序设置:通过application.properties

中的Spring Boot配置实现
//SpringBoot 2.1.2.RELEASE
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB

2)服务器设置:此设置位于应用程序设置的之上。在Spring Boot中,可以通过两种方式完成此服务器配置

2.1)在嵌入式tomcat中,如以下链接EMBEDDED_TOMCAT_MAX_SHALLOW_SIZE所示(部署.war文件时,这是不够的)(无需在2.1.2中执行此操作。发布

2.2)在独立服务器中:在文件{TOMCAT_HOME}/conf/server.xml中找到部分<Connector port=“8080"并添加属性maxSwallowSize=“-1"/>

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443"
           maxSwallowSize="-1"/>

使用-1,您将在服务器上禁用尺寸控制,现在只有Spring应用程序上的设置很重要

此外,因为您正在使用文件,所以我认为您应该设置最适合您的API的MediaType(在我的情况下,我有文件+其他json元素)

@PostMapping(value = "/path", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

并在调用REST-API时尊重标头

headers.setContentType(MediaType.MULTIPART_FORM_DATA_VALUE)

答案 2 :(得分:1)

一个疯狂的猜测-您可能正在使用Spring Boot 1.x,并且可能正在尝试利用Spring Boot 2.x中的设置。

为实现Spring Boot v1.x所需设置的正确属性如下:

spring.http.multipart.max-file-size=-1
spring.http.multipart.max-request-size=-1
spring.http.multipart.enabled=true

我的猜测基于以下事实:您在config / properties文件中设置了不同的文件大小,但似乎您遇到了默认值。