Reputation: 233
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:
I use postman with like this to insert i file:
Upvotes: 11
Views: 24019
Reputation: 3166
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)
Upvotes: 8
Reputation: 15878
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
Upvotes: 9
Reputation: 662
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.
Upvotes: 4