How to increase file size upload limit in spring boot using embedded tomcat

M Swapnil picture M Swapnil · Mar 6, 2020 · Viewed 7.7k times · Source

I am try to upload the file using my spring boot API. The function is working fine when I am using small file (less than 1 MB), but when I upload large file it gives me an exception. I am using embedded Tomcat server.

"Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes."

I have tried the following code in my files but every time I am getting the error

1. application.property

server.tomcat.max-swallow-size=100MB

server.tomcat.max-http-post-size=100MB

spring.servlet.multipart.enabled=true

spring.servlet.multipart.fileSizeThreshold=100MB

spring.servlet.multipart.max-file-size=100MB

spring.servlet.multipart.max-request-size=100MB

I have also tried

spring.servlet.multipart.maxFileSize=100MB

spring.servlet.multipart.maxRequestSize=100MB

2. The belove is my file uploading code

public RestDTO uploadFile(MultipartFile file, String subPath) {

    if (file.isEmpty()) {
        return new RestFailure("Failed to store empty file");
    }

    try {
        String fileName = new Date().getTime() + "_" + file.getOriginalFilename();
        String filePath = uploadPath + subPath + fileName;
        if (Objects.equals(file.getOriginalFilename(), "blob")) {
            filePath += ".png";
            fileName += ".png";
        }
        File uploadDir = new File(uploadPath + subPath);
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }
        FileOutputStream output = new FileOutputStream(filePath);
        output.write(file.getBytes());
        LOGGER.info("File path : " + filePath);

        MediaInfoDTO mediaInfoDTO = getThumbnailFromVideo(subPath, fileName);

        String convertedFileName = convertVideoToMP4(subPath, fileName);

        System.out.println("---------------->" + convertedFileName);

        return new RestData<>(new MediaDetailDTO(mediaInfoDTO.getMediaPath(), convertedFileName,
                mediaInfoDTO.getMediaType(), mediaInfoDTO.getMediaCodec(), mediaInfoDTO.getWidth(),
                mediaInfoDTO.getHeight(), mediaInfoDTO.getDuration()));
    } catch (IOException e) {
        LOGGER.info("Can't upload file: " + e.getMessage());
        return new RestFailure("Failed to store empty file");
    }
}

but every time I got the same exception.

Answer

Milan Desai picture Milan Desai · Mar 6, 2020

Apart from comment might I suggest creating a @Bean for Factory MultipartConfigurationElement This basically should override other restrictions if you have any from TomCat side.

@Bean
public MultipartConfigElement multipartConfigElement() {
    MultipartConfigFactory factory = new MultipartConfigFactory();
    factory.setMaxFileSize(DataSize.ofBytes(100000000L));
    factory.setMaxRequestSize(DataSize.ofBytes(100000000L));
    return factory.createMultipartConfig();
}

Here DataSize is of type org.springframework.util.unit.DataSize

Reference https://github.com/spring-projects/spring-boot/issues/11284

Another issue I suspect could be from TomCat maxSwallowSize see Baeldung's point #5 if above does not work. https://www.baeldung.com/spring-maxuploadsizeexceeded