问题
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:
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());
}
来源:https://stackoverflow.com/questions/59388757/getting-files-in-controller-without-requestparam-name