获取所有上传的MultipartFile文件的集合

时间:2017-01-25 18:52:14

标签: java spring file-upload

我正在寻找像key => MultipartFile这样的Spring中所有上传文件的地图。到目前为止,我发现的那些要求我知道文件的key

有人知道吗?

2 个答案:

答案 0 :(得分:0)

事实证明,这很简单:

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public ResponseEntity executeByPost(WebRequest webRequest)
{
    MultiValueMap<String, MultipartFile> files = ((StandardMultipartHttpServletRequest) ((ServletWebRequest) webRequest).getRequest()).getMultiFileMap();
    return ResponseEntity.ok(files.keySet());
}

答案 1 :(得分:0)

你可以收集像这样的部分:

@RequestMapping(value = "/loadFile")
@ResponseBody
public String handleTestRequest (MultipartHttpServletRequest r) throws IOException, ServletException {
    for (Part part : r.getParts()) {
        String name = part.getName();
        String result = new BufferedReader(new InputStreamReader(part.getInputStream()))
                  .lines().collect(Collectors.joining("\n"));

        System.out.println(name + " " + result);

    }
    return "test output";
}