java - 使用 Spring 4 RestTemplate 上传 MultipartFile 列表(Java 客户端和 RestController)

标签 java spring spring-mvc multipartform-data resttemplate

我正在尝试使用 Spring RestTemplate 将 MultipartFile 列表发布到我的 RestController,尽管我对用于我的客户端和 Controller 的确切语法和类型有点困惑。这是我迄今为止根据我所做的研究得出的结论......

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 发送的 request 负载与服务器期望的负载不匹配。您可以尝试更改以下内容:

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

更新

上传多个文件时,需要确保ByteArrayResourcegetFileName每次返回相同的值。如果没有,您将始终得到一个空数组。

例如以下对我有用:

客户:

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 - 使用 Spring 4 RestTemplate 上传 MultipartFile 列表(Java 客户端和 RestController),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42212557/

相关文章:

java - onTouch 与多点触控和 SoundPool 运动事件

java - 读取android系统文件

java - 如何在 Java 中解析 JSON 对象的精确值?

java - 无法打印整个字符串

java - 如何配置 Thymeleaf 模板位置

java - 实现依赖注入(inject)的最简单方法是什么?

java - 重启后 Config Server 的多个请求

java - 在 Spring 中,如何从 HTTPServletResponse 对象获取 HTML 作为字符串?

java - Spring MVC 形式的两个模型

java - Spring Boot找不到主类多模块