简体   繁体   English

MultipartFile 不在“@RequestParam Map”中<String, Object> requestParams”变量

[英]MultipartFile not in “@RequestParam Map<String, Object> requestParams” variable

So I'm currently building a Multipart-Fileupload with Spring.所以我目前正在用 Spring 构建一个 Multipart-Fileupload。 I want to create my Controller as generic as it can be to enable a good start for my own modulized-controller-application.我想尽可能通用地创建我的控制器,以便为我自己的模块化控制器应用程序提供一个良好的开端。 That's why I'm working with @RequestParam Map<String, Object> requestParams .这就是我使用@RequestParam Map<String, Object> requestParams So my dummy function looks like this:所以我的虚拟函数如下所示:

@RequestMapping(path = "uploadtest", method = POST, consumes = MULTIPART_FORM_DATA_VALUE)
    public String test(@RequestParam("file") MultipartFile file,
                       @RequestParam Map<String, Object> requestParams) {
        return "/[...]";
    }

Now when I post a file, I would asume, that it would be available through the file -variable (works, ok.) and through requestParams.get("file") (doesn't work).现在,当我发布一个文件时,我会假设它可以通过file -variable(有效,好的)和requestParams.get("file") (不起作用)获得。

So here is my question:所以这是我的问题:

Is this intendet to work like this, or is there just no multipart-support enabled for the general @RequestParams annotation (=Bug/Feature?).这个意图是这样工作的,还是没有为一般的@RequestParams注释(=Bug/Feature?)启用多部分支持。

A multipart request consist of several parts.多部分请求由几个部分组成。 In case of multipart/form-data the first part is supposed to consist of parameter / value pairs that are also available as @RequestParam .multipart/form-data的情况下,第一部分应该由参数/值对组成,也可以作为@RequestParam The other parts are the files.其他部分是文件。

When you have @RequestParam MultipartFile file Spring knows that you want the part that is associated with the file parameter.当您拥有@RequestParam MultipartFile file Spring 知道您想要与file参数关联的部分 Since the file is a part you can also use @RequestPart("file") MultipartFile file .由于文件是一部分,您还可以使用@RequestPart("file") MultipartFile file

Keep in mind that request parameters are Strings.请记住,请求参数是字符串。 So Map<String, Object> is basically the same as Map<String, String> .所以Map<String, Object>Map<String, String>基本相同。 Therefore requestParams.get("file") will give you a String value of the file parameter from the first part of the request.因此requestParams.get("file")将从请求的第一部分为您提供file参数的字符串值。

"I want to create my Controller as generic as it can be" You rather want it as specific as it can be. “我想尽可能通用地创建我的控制器”您宁愿希望它尽可能具体。 If you want to keep things generic, then don't use controllers in the first place.如果你想保持通用性,那么首先不要使用控制器。

I founded greate solution to get file map as Map<String, MultipartFile> fileMap .我创建了伟大的解决方案来获取文件映射为Map<String, MultipartFile> fileMap You should use : multiRequest.getFileMap();你应该使用: multiRequest.getFileMap(); from object HttpServletRequest.来自对象 HttpServletRequest。

    @PostMapping(consumes = {MULTIPART_FORM_DATA_VALUE})
    ResponseEntity<String> createDocument(@RequestParam Map<String, String> allParams, HttpServletRequest request)

Map<String, MultipartFile> fileMap = new HashMap<String, MultipartFile>();
if (request instanceof MultipartHttpServletRequest) {
    MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
    fileMap = multiRequest.getFileMap();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM