gpt4 book ai didi

java - 如何使用resttemplate将多部分文件和一些字段传递到另一个api?

转载 作者:行者123 更新时间:2023-12-02 09:37:08 26 4
gpt4 key购买 nike

我想将多部分文件、用户名和电子邮件地址传递给另一个休息 Controller 。当我尝试时,收到错误“所需的请求不是多部分文件”。

其余模板代码:

    val headers = LinkedMultiValueMap<String, String>()
headers["x-apikey"] = apiKey
headers["content-type"] = "multipart/form-data"

println("updateFileHeaderStatusByIdFlowUrl : " + uploadCsvFileFlowUrl)

val body = LinkedMultiValueMap<String, Any>()
body.add("multipartFile", multipartFile)
body.add("backofficeUsername", backofficeUsername)
body.add("backofficeUserEmailAddress", backofficeUserEmailAddress)

val requestEntity = HttpEntity<LinkedMultiValueMap<String, Any>>(body, headers)


var response = restTemplate.exchange("localhost:8080/uploadCSVFile",
HttpMethod.POST, requestEntity, String::class.java)

System.out.println("response status: " + response.getStatusCode())
System.out.println("response body: " + response.getBody())

其余API:

     @PostMapping("/uploadCSVFile/")
fun uploadCSVFile(@RequestParam multipartFile: MultipartFile,
@RequestParam backofficeUsername: String,
@RequestParam backofficeUserEmailAddress: String) {}

最佳答案

尝试在方法签名中添加multipart/form-data

@PostMapping("/uploadCSVFile/", consumes = { MediaType.MULTIPART_FORM_DATA })
fun uploadCSVFile(@RequestParam multipartFile: MultipartFile,
@RequestParam backofficeUsername: String,
@RequestParam backofficeUserEmailAddress: String) {}

关于java - 如何使用resttemplate将多部分文件和一些字段传递到另一个api?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57408726/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com