Getting files in controller without @RequestParam name

本秂侑毒 提交于 2021-01-07 07:35:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!