在没有@RequestParam名称的控制器中获取文件 [英] Getting files in controller without @RequestParam name
问题描述
我正在尝试构建通用的POST控制器.我可以通过通用方式访问参数,但是如果不使用@RequestParam("files")MultipartFile []文件就无法获得可能的文件,那么,有人知道如何以通用方式获取文件(如果有提交的话)吗?
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...
}
为了清楚起见,我不想设置(文件"),因为它可以使用任何名称上传.我知道我可以使用@RequestParam,但我不能没有名字.谢谢:)
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 :)
推荐答案
对于想知道如何执行的人:就像注入HttpServletRequest请求并使用
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());
}
这篇关于在没有@RequestParam名称的控制器中获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!