使用Spring 4 restTemplate(Java Client& RestController)上传MultipartFile列表 [英] Uploading a List of MultipartFile with Spring 4 restTemplate (Java Client & RestController)

查看:134
本文介绍了使用Spring 4 restTemplate(Java Client& RestController)上传MultipartFile列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用spring restTemplate将MultipartFile列表发布到我的RestController,尽管我对确切的语法&我的客户使用的类型和控制器。到目前为止,根据我所做的研究,这是我所拥有的...

I am trying to post of List of MultipartFile to my RestController using spring restTemplate although I'm a bit confused as to the exact syntax & types to use for my client & controller. Here's what I have so far based on the research I've done...

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属性有一个空映射。我看了很多帖子试图解决这个问题,但无济于事。任何帮助将不胜感激。

If I understand it correctly. The multipart filter should parse my MultiValueMap into a List of MultipartFiles and a MultipartHttpServletRequest? The only way I can get my client to hit my RestController is to send the file data as a ByteArrayResource, however in my Controller my RequestBody is always null and the MultipartHttpServletRequest has an empty map for its multipartFiles attribute. I've looked at numerous posts to try to resolve this issue but to no avail. Any help would be much appreciated.

推荐答案

它看起来像请求您从 FileUploadClient 发送的有效负载与服务器的预期不匹配。您可以尝试更改以下内容:

It looks like the request payload that you are sending from FileUploadClient does not match what's server is expecting. Could you try changing the following:

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);

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

Also, could you try changing the server's method signature to the following:

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

更新

上传多个文件时,您需要确保 getFileName ByteArrayResource 每次都返回相同的值。如果没有,你将永远得到一个空数组。

While uploading multiple files, you need to make sure getFileName of ByteArrayResource returns same value every time. If not, you will always get an empty array.

例如。以下内容适用于我:

E.g. the following works for me:

客户:

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){

这篇关于使用Spring 4 restTemplate(Java Client&amp; RestController)上传MultipartFile列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆