Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the maximum size for a Multipart file in spring boot?

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

like image 209
flyordie Avatar asked Oct 10 '18 13:10

flyordie


1 Answers

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

like image 80
Alien Avatar answered Sep 20 '22 13:09

Alien