Warm tip: This article is reproduced from serverfault.com, please click

Issue with Tomcat and parsing MultipartFile Upload service

发布于 2020-12-05 21:53:19

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

Questioner
Kamarudeen Ayankunbi
Viewed
0
Kamarudeen Ayankunbi 2020-12-07 09:37:15

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.

Spark Java: Unable to process parts as no multi-part configuration has been provided