首页 > 解决方案 > 发送列表从一项服务到另一项服务给出“当前请求不是多部分请求”异常

问题描述

@RestController
@RequestMapping(values = ["/rest/send"])
class Controller {

@PostMapping
fun send(@RequestParam(value ="id") id: String, @RequestParam(value="files") files: List<MultipartFile): String {
val headers = HttpHeaders()
headers.contentType = MediaType.MULTIPART_FORM_DATA

val reqBody = LinkedMultiValueMap<String,Any>()
reqBody.add("id",id)
for(file in files) {
val resource: ByteArrayResource = object ByteArrayResource(file.bytes) {
override fun getFileName():String() {
return ""
}
reqBody.add("files",resource)
}
val reqEntity =  HttpEntity(reqBody,headers)
val restTemplate = RestTemplate()
restTemplate.postForEntity<String>(url,reqEntity,String::class.java)
return "sent"
}
}

标签: javaspring-boothttpkotlinmultipart

解决方案


推荐阅读