krise
krise

Reputation: 535

Changing the size-limit for file upload on spring boot 2.1.2

Im working on a project with angular frontend and springboot 2.1.2 backend and have implemented a way to upload images. However it only works if the image is < 1MB, bigger images cause an exception:

org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field image exceeds its maximum permitted size of 1048576 byte

I have already looked through these question: I am trying to set maxFileSize but it is not honored Spring upload file size limit

I tried adding

spring.servlet.multipart.max-File-Size: 30MB 
spring.servlet.multipart.max-Request-Size: 30MB

and when that didnt work also

spring.servlet.multipart.maxFileSize: 30MB
spring.servlet.multipart.maxRequestSize: 30MB

to my application.yml, neither of which seemed to do anything.

I also found this: https://www.baeldung.com/spring-maxuploadsizeexceeded

and tried setting the limit as described in point 2 of that site.

None of that did the trick. Any ideas what I am doing wrong? Here is my application.yml:

logging:
  file: ./log/backend.log
  level.: WARN

banner:
  location: banner/banner.txt

server:
  context-path: /

spring:
  application:
    name: Backend
  jpa:
    hibernate:
      ddl-auto: validate
      use-new-id-generator-mappings: true
  profiles:
    active: development
  http:
    multipart:
      maxFileSize: 30MB
      maxRequestSize: 30MB

security:
  basic:
    enabled: false

management:
  info:
    git:
      mode: full

Here is my controller:

@RestController
@RequestMapping(value = "/api/v1/image")
@Api(value = "Image")
public class ImageEndpoint {

    @Autowired
    private ImageMapper imageMapper;
    @Autowired
    private IImageService imageService;

    @RequestMapping(method = RequestMethod.POST)
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation(value = "Upload a new image", authorizations = {@Authorization(value = "apiKey")})
    @ResponseStatus(HttpStatus.OK)
    public @ResponseHeader Long post(@RequestParam ("image") MultipartFile input){
        try {
            return imageService.save(imageMapper.dtoToEntitiy(new ImageDto(input.getBytes())));
        }catch (IOException io){
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
        }
    }

Update:

  servlet:
    multipart:
      max-request-size: 30MB
      max-file-size: 30MB

this made it work, I just recompiled after adding it at first, so the changes didn't get through, I actually just had to rebuild to make it work.

Upvotes: 2

Views: 4193

Answers (1)

Sambit
Sambit

Reputation: 8031

Can you try with the following configuration in properties file ?

multipart.max-file-size=30MB
multipart.max-request-size=30MB

In case of yml configuration, add the below settings.

spring:
 http:
  multipart:
   max-file-size: 30MB
   max-request-size: 30MB

To upload a file with unlimited length, you can set the below.

spring.http.multipart.max-file-size=-1

Hope it works for you.

Upvotes: 4

Related Questions