使用Spring 4 restTemplate(Java Client和RestController)上传MultipartFile列表

我试图使用spring restTemplate将MultipartFile列表发布到我的RestController,尽管我对确切的语法和放大器有点困惑.我的客户使用的类型和控制器.基于我所做的研究,到目前为止我所拥有的……

FileUploadClient.java

public void uploadFiles(List<MultipartFile> multiPartFileList) throws IOException {
    MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
    List<Object> files = new ArrayList<>();
    for(MultipartFile file : multiPartFileList) {
        files.add(new ByteArrayResource(file.getBytes()));
    }
    map.put("files", files);

    // headers is inherited from BaseClient
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
    ResponseEntity<String> response = restTemplate.exchange(restURI + "/rest/fileupload/uploadfiles", HttpMethod.POST, request, String.class);
    if(HttpStatus.OK.equals(response.getStatusCode())) {
        System.out.println("status for /rest/fileupload/uploadfiles ---> " + response);
    }
}

FileUploadRestController.java

@RequestMapping(value = "/uploadfiles", method = RequestMethod.POST)
public ResponseEntity<?> uploadFiles(@RequestParam("files") List<MultipartFile> files, HttpServletRequest request) {
    ResponseEntity<?> response;
    try {
        // do stuff...
        response = new ResponseEntity<>(header, HttpStatus.OK);
        System.out.println("file uploaded");
    } catch (Exception e) {
        // handle exception
    }
    return response;
}

web.xml中

<filter>
    <filter-name>multipartFilter</filter-name>
    <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>multipartFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

为spring-servlet.xml

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- max upload size in bytes -->
    <property name="maxUploadSize" value="20971520" /> <!-- 20MB -->
    <!-- max size of file in memory (in bytes) -->
    <property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->
</bean>

如果我理解正确的话.多部分过滤器应该将我的MultiValueMap解析为MultipartFiles列表和MultipartHttpServletRequest?
我可以让我的客户端访问我的RestController的唯一方法是将文件数据作为ByteArrayResource发送,但是在我的Controller中,我的RequestBody始终为null,而MultipartHttpServletRequest的multipartFiles属性则为空映射.我看了很多帖子试图解决这个问题,但无济于事.任何帮助将非常感激.

解决方法:

看起来您从FileUploadClient发送的请求有效负载与服务器期望的不一致.您可以尝试更改以下内容:

MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
for(MultipartFile file : multiPartFileList) {
    map.add(file.getName(), new ByteArrayResource(file.getBytes()));
}

MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
List<ByteArrayResource> files = new ArrayList<>();
for(MultipartFile file : multiPartFileList) {
    files.add(new ByteArrayResource(file.getBytes()));
}
map.put("files", files);

此外,您是否可以尝试将服务器的方法签名更改为以下内容:

public ResponseEntity<?> uploadFiles(@RequestParam("files") List<MultipartFile> files, HttpServletRequest request) {

更新

在上传多个文件时,您需要确保ByteArrayResource的getFileName每次都返回相同的值.如果没有,您将始终获得一个空数组.

例如.以下为我工作:

客户:

MultiValueMap<String, Object> data = new LinkedMultiValueMap<String, Object>(); 
for(MultipartFile file : multiPartFileList) { 
    ByteArrayResource resource = new ByteArrayResource(file.getBytes()) { 
        @Override 
        public String getFilename() { 
            return ""; 
        } 
    }; 
    data.add("files", resource);
} 

服务器

public ResponseEntity<?> upload(@RequestParam("files") MultipartFile[] files){
上一篇:java-org.springframework.web.client.RestClientException:无法提取响应:


下一篇:java – 使用RestTemplate设置安全性cookie