java spring rest_使用Spring 4 restTemplate(Java Client和RestControlle...

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

FileUploadClient.java

public void uploadFiles(List multiPartFileList) throws IOException {

MultiValueMap map = new LinkedMultiValueMap<>();

List 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> request = new HttpEntity<>(map, headers);

ResponseEntity 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 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中

multipartFilter

org.springframework.web.multipart.support.MultipartFilter

multipartFilter

/*

为spring-servlet.xml

如果我理解正确的话.多部分过滤器应该将我的MultiValueMap解析为MultipartFiles列表和MultipartHttpServletRequest?

我可以让我的客户端访问我的RestController的唯一方法是将文件数据作为ByteArrayResource发送,但是在我的Controller中,我的RequestBody始终为null,而MultipartHttpServletRequest的multipartFiles属性则为空映射.我看了很多帖子试图解决这个问题,但无济于事.任何帮助将非常感激.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值