AgoYaMo
AgoYaMo

Reputation: 63

Getting files in controller without @RequestParam name

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 :)

Upvotes: 2

Views: 843

Answers (1)

AgoYaMo
AgoYaMo

Reputation: 63

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());
}

Upvotes: 4

Related Questions