0

I have a spring boot (v 2.7.1) application with mongodb, and i have an api to upload a file

application.yml

spring:
  servlet:
    multipart:
      enabled: true
      max-file-size: 200MB
      max-request-size: 200MB

MultipartResolver Bean

@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    return multipartResolver;
}

dependencies

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.2.1</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>

service

//define additional metadata
DBObject metadata = new BasicDBObject();
metadata.put("fileSize", file.getSize());

//store in database which returns the objectID
gridFsTemplate.store(file.getInputStream(), file.getOriginalFilename(), file.getContentType(), metadata);

the problem i get an error when i get inputStream from MultipartFile file.getInputStream()

enter image description here

Aymen Kanzari
  • 1,765
  • 7
  • 41
  • 73
  • can you check if you are getting correct request parameter as input in your controller? example here from frontend, form param file is taken as input https://www.baeldung.com/spring-file-upload#uploading – swapyonubuntu Oct 06 '22 at 12:51

0 Answers0