Reputation: 427
I have an app with an endpoint and a tomcat server running with it. I can hit an endpoint on postman with a file and have it uploaded to an excel file on my server.
The problem is in order to get it to work I have to add allowCasualMultipartParsing="true" in the context.xml of my tomcat I don't want to have to add this fix on my local tomcat.
I need a fix that will work despite the tomcat server it's running on. So if someone were to add a new tomcat they wouldn't have this issue. For example on tomcat 9.0.6
Its hard to demonstrate the issue because it's due to running the app on a standalone tomcat. It works when I run it without the standalone tomcat 9.0.38 .
Code for uploading file
@Controller
@RequestMapping("/ListCtrl")
public class listController {
@RequestMapping(method = {RequestMethod.POST}, value = "/list")
@Consumes (MediaType.MULTIPART_FORM_DATA)
@Produces (MediaType.TEXT_XML)
@ResponseBody public Map<String, Object> uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") MultipartFile file,
@RequestParam("listName") String listName,
@RequestParam Integer
listid){
Map<String, Object> resultMap = null;
resultMap = new HashMap<>();
resultMap.put("status", "successful");
resultMap.put("file", file.getName());
System.out.println(file.getOriginalFilename());
return resultMap;
}
The error I'm getting on the project that isn't working is HTTP Status 500 - Could not parse multipart servlet request; nested exception is java. lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided
Upvotes: 0
Views: 2780
Reputation: 427
I found the solution. I've seen it before but didn't fully understand so I wanted to elaborate here in case anyone else sees it. This solution will work despite the tomcat its running on.
You need to create a META-INF folder. For me I created under a deployed resources folder and I added a context.xml. After I put allowCasualMultipartParsing="true"
in the Context
tag.
Webapp/Deployed Resources/META-INF/context.xml.
<Context allowCasualMultipartParsing="true"/>
Spark Java: Unable to process parts as no multi-part configuration has been provided
Upvotes: 2
Reputation: 1754
It is enough if you provide a configuration. As it has been mentioned in the spring documentation, there are two concrete implementations included in Spring.
For better flexibility and configurabilities sake, I choose to use CommonsMultipartResolver. Among the advantages, it provides maxUploadSize, maxInMemorySize, and defaultEncoding settings as bean properties. But, you have to import it as:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
And in your code:
@Configuration
public class WebConfig {
private int maxUploadSizeInMb = 2 * 1024 * 1024; // 2 MB
...
...
@Bean("multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver cmr = new CommonsMultipartResolver();
cmr.setMaxUploadSize(maxUploadSizeInMb * 2); //sum size of all files/parts of a file. Since, a file may be partitioned
cmr.setMaxUploadSizePerFile(maxUploadSizeInMb);//maximum file size of each file
return cmr;
}
}
Upvotes: 0