For some reason I am not able to override Tomcat's settings about the file size limit when working with the UAT profile environment (everything works smoothly on SIT)
I am using Spring Boot 2.2 and i am adding these properties to the configuration file:
spring.servlet.multipart.max-file-size=25MB
spring.servlet.multipart.max-request-size=25MB
However when i run my application on the Eclipse IDE (clean install and profile UAT) it gives me errors when I try to upload files which are greater than 1MB (which AFAIK is the default Tomcat's file limit size).
A couple more things to point out:
- Both the application-sit.properties file and the application-uat.properties have all of the instructions placed in the same order.
- When I try to upload file whose size is lower than 1MB i get another error which is UnsupportedMediaType and so I was wondering whether the problem was in my Angular front-end or in my Spring Boot back-end application.
Oh... and I run my SIT application locally while the UAT application is run in a VM (it wouldn't surprise me if it is something that has to do with the VM).
Thank you all in advance for the help, I'm open to answer if you need more informations.
Have a nice day.
UPDATE
As requested by @PiotrP.Karwasz I am including the Angular and Spring code I used to make the upload service:
upload()
in upload-service.service.ts
upload(file: File) {
const formData: FormData = new FormData();
formData.append('file', file);
return new Observable(((observer: Observer<any>) => {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = _ => {
if (xhr.readyState === 4) {
if(xhr.status === 200) {
observer.next(xhr.response);
observer.complete();
}
else {
observer.error(xhr.response);
}
}
};
///environment.baseUrl changes based on the active profile config
let url = environment.baseUrl constants.REST_UPLOAD_FILE;
xhr.open('POST', url, true);
xhr.send(formData);
}));
}
save()
in FileStorageService.java
@Override
public void save(MultipartFile file) {
try {
String filename = FilenameUtils.removeExtension(file.getOriginalFilename())
" - "
new SimpleDateFormat("dd꞉MM꞉yyyy HH'h'꞉mm'min'꞉ss's'")
.format(new Date())
.toString()
.replaceAll("[^a-zA-Z0-9.-⁄꞉]", "_")
"." FilenameUtils.getExtension(file.getOriginalFilename());
Files.copy(file.getInputStream(), this.root.resolve(filename));
} catch (final Exception e) {
throw new RuntimeException("Non posso salvare il file. Errore: " e);
}
}
Note that as far as I understood by making tests printing stuff on the Eclipse console it seems like it doesn't even get to the point of reaching the Spring Boot save()
method and I've actually also tried printing things on the controller method (whose code will be linked below) that manages the incoming requests but it doesn't show anything either way so seems more like it's never reaching the controller too.
The controller method who takes care of the upload request is available at https://pastebin.com/EMZStXJw
CodePudding user response:
Okay, after extracting the .war
file of my front-end build I finally got it and I'll put this here for anyone who may need it.
The thing is that my environment.baseUrl
(see the upload()
method in upload-service.service.ts
file) was pointing to an invalid endpoint (I still have to figure out why the endpoint was behaving like that and why it was there in the first place) and so when I tried making the request i got ambigous errors that had nothing to do with the actual one.
Modifying the UAT configuration file was enough to make it work.
TLDR;
Make sure you are pointing to the right URL endpoint in your UAT
configuration file and extract the .war
to check your application.properties
files.