In my Spring MVC project I am trying to upload a file via a simple form.
HTML Form:
For me even with the csrf().disable() its didn't work. Once i disable it i got 200 but the file wasn't uploaded and i didn't see any error. Once i set the debug flag logging.level.org.springframework.web: DEBUG i saw the root cause:
[org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location [/target/tomcat/work/Tomcat/localhost/ROOT] is not valid]","exception":""}
I tried setting the location on application.yaml: spring.servlet.http.multipart.location: "/tmp" but it didn't change the location so what i did is adding the below code and it did the trick:
@Bean
MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
String location = "/tmp";
LOGGER.debug("Multipart location file:" + location);
File tmpFile = new File(location);
if (!tmpFile.exists()) {
tmpFile.mkdirs();
}
factory.setLocation(location);
return factory.createMultipartConfig();
}