2

I'm trying to build a generic POST controller. I can access params in a generic way but I can't get possible files without using @RequestParam("files") MultipartFile[] files so, does anyone know how to get files (if there are any submitted) in a generic way?

This is my controller so far:

@PostMapping
public void save(@RequestParam MultiValueMap<String, Object> o)   {
    Iterator it = o.keySet().iterator();
    while(it.hasNext()){
        String key = (String) it.next();
        String value = (String) o.getFirst(key);
        System.out.println("Key: " + key + " Value: " + value);
    }
    etc...
}

To be clear, I don't want to set ("files") because it can be uploaded with any name. I know I can use @RequestParam but I can't without a name. Thank you :)

1 Answer 1

4

To whoever wants to know how to perform it: It's as easy as to inject HttpServletRequest request and use

Map<String, MultipartFile> multipartFiles = ((MultipartHttpServletRequest) request).getFileMap();
for (Entry<String, MultipartFile> file : multipartFiles.entrySet()) {
    o.put(file.getKey(), file.getValue());
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.