10

I try to upload a song with postman but I have a error like the The field song exceeds its maximum permitted size of 1048576 bytes."

I already tried to add this configurations in the application.properties files but it doesn't work :

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

I precise that's work with files which size is < 1048576 bytes

This is my Controller:

@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");
    }

This is my service:

@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()));
            }
        }
    }

This is my message with the error: {"timestamp":"2018-10-10T13:27:07.090+0000","status":500,"error":"Internal Server Error","message":"Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field song exceeds its maximum permitted size of 1048576 bytes.","path":"/music-service/songs"}

I use microservices and my configuration is on gitLab and is this one:

enter image description here

I use postman with like this to insert i file:

enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
flyordie
  • 213
  • 2
  • 3
  • 14

3 Answers3

9

For spring-boot version 2.x.x give a try with below code by including http.

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

Refer set-maxfilesize

Alien
  • 15,141
  • 6
  • 37
  • 57
  • 1
    Instead of voting -1 my answer read the post, he already has these settings. – ValerioMC Oct 10 '18 at 14:16
  • 1
    @ValerioMC - setings are not the same, plase notice the spring.http.* vs spring.servlet.*. However, his answer is also misleading the OP, because v2.x versions (aside from i.e. very early 2.x beta releases) utilize settings that OP posted, while the spring boot v1.x versions utilize the spring.http.* settings. However, the most probable answer is that OP utilizes spring boot v1.x and these settings should work, regardless of the fact that Alien made an omission himself – SadmirD Oct 10 '18 at 14:27
  • 2
    this method i think is depreciated for spring 2.0 `Deprecated configuration property 'spring.http.multipart.max-file-size' more... (Ctrl+F1)` – flyordie Oct 10 '18 at 14:41
8

UPDATED SpringBoot 2.0.0.RELEASE

Multipart size in a web application depends on two separate settings

1) application settings: achieved by Spring Boot configuration in application.properties

//SpringBoot 2.1.2.RELEASE
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB

2) server settings: this setting is above the application settings. And in Spring Boot this server configuration can be done in two ways

2.1) In embedded tomcat, as you have found at the following link EMBEDDED_TOMCAT_MAX_SHALLOW_SIZE (this is not sufficient when deploying .war file) (no need to do this in 2.1.2.RELEASE)

2.2) In standalone server: In file {TOMCAT_HOME}/conf/server.xml find part <Connector port=“8080" and add property maxSwallowSize=“-1"/>

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

With -1 you are disabling size control on Server, and now only settings on your spring app matters

Moreover because you are working with files I think you should set your MediaType that best fits your API (in my case i had file + additional json elements)

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

And respect the header when calling REST-API as

headers.setContentType(MediaType.MULTIPART_FORM_DATA_VALUE)
Youans
  • 4,801
  • 1
  • 31
  • 57
ValerioMC
  • 2,926
  • 13
  • 24
  • I use rest api , so i test with postman i think i don't need form `method="POST" enctype="multipart/form-data"` – flyordie Oct 10 '18 at 14:37
  • i see. try to change `maxShallowSize` and it should work. – ValerioMC Oct 10 '18 at 15:00
  • do you have any idea how to change it? because there is no propertie for that , i saw this tuto: https://www.mkyong.com/spring-boot/spring-boot-configure-maxswallowsize-in-embedded-tomcat/ – flyordie Oct 10 '18 at 15:06
  • the link you send is for embedded tomcat, still it will not work if you deploy your application on external server. updated answer with details on how to change `maxSwallowSize` – ValerioMC Oct 10 '18 at 15:13
  • but for the moment i will work with a tomcat embedded .Tomorrow i will test it and i come back with the answer – flyordie Oct 10 '18 at 19:15
4

A wild guess - you could be using spring boot 1.x and you may be trying to utilize settings from spring boot 2.x.

Correct properties for achieving desired settings for spring boot v1.x would be the following:

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

My guess is based on the fact that you have set a different file size in your config/properties file and yet it seems you encounter default value.

SadmirD
  • 642
  • 3
  • 8