简体   繁体   English

Spring 引导 - 在不知道关键参数的情况下通过 POST 请求在 controller 中获取上传的文件?

[英]Spring Boot - get uploaded files in a controller without knowing key parameters with POST request?

I use Spring Boot 1.5.2 and I have a controller to handle a POST request which can contain a random uploaded files as binary and random keys values as string with different POST parameters.我使用 Spring Boot 1.5.2 并且我有一个 controller 来处理一个 POST 请求,它可以包含一个随机上传的文件作为二进制文件随机键值作为具有不同 POST 参数的字符串

For example:例如:

curl -F 'fileX=@/path/to/fileX' 
     -F 'fileY=@/path/to/fileY'
     -F 'abc=@/path/to/fileZ' 
     -d "key1=value1"
     -d "key2=value2"

http://localhost/upload

I've searched for MultipartFile[] , but it seems it needs to have a fixed key parameter, otherwise this files variable will be null, eg:我已经搜索了MultipartFile[] ,但它似乎需要有一个固定的关键参数,否则这个文件变量将是 null,例如:

@Controller
public class FilesController {

    @PostMapping("/upload")
    public void handlePost(HttpServletRequest httpServletRequest, 
                           HttpServletResponse httpServletResponse, 
                           @RequestParam("filesKey") MultipartFile[] files) {
 
    }
}

How can I get the list of posted files in this case without knowing the key parameters of them?在这种情况下,如何在不知道关键参数的情况下获取已发布文件的列表?

I think the uploaded files can be achieved like this below:我认为上传的文件可以像下面这样实现:

@PostMapping("/upload")
public void handlePost(HttpServletRequest httpServletRequest, 
                       HttpServletResponse httpServletResponse) {

        for (Part part : httpServletRequest.getParts()) {
            System.out.println(part.getContentType());
            System.out.println(part.getName());
        }

}

which will output:这将是 output:

application/octet-stream
fileX
application/octet-stream
fileY
application/octet-stream
abc
null
key1

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

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