gpt4 book ai didi

spring - 如何通过 RestTemplate 发布字节数组

转载 作者:行者123 更新时间:2023-12-04 03:13:05 38 4
gpt4 key购买 nike

目标:使用 RestTemplate 发布图像

目前正在使用这个的变体

MultiValueMap<String, Object> parts = new
LinkedMultiValueMap<String, Object>();
parts.add("field 1", "value 1");
parts.add("file", new
ClassPathResource("myFile.jpg"));
template.postForLocation("http://example.com/myFileUpload", parts);

有没有其他选择?发布包含 base64 编码的 byte[] 数组的 JSON 是否是有效的替代方案?

最佳答案

是的,我猜有这样的事情

如果图像是您的有效负载,并且您想调整标题,则可以通过以下方式发布:

HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "image/jpeg");
InputStream in = new ClassPathResource("myFile.jpg").getInputStream();

HttpEntity<byte[]> entity = new HttpEntity<>(IOUtils.toByteArray(in), headers);
template.exchange("http://example.com/myFileUpload", HttpMethod.POST, entity , String.class);

否则:
InputStream in = new ClassPathResource("myFile.jpg").getInputStream();
HttpEntity<byte[]> entity = new HttpEntity<>(IOUtils.toByteArray(in));
template.postForEntity("http://example.com/myFileUpload", entity, String.class);

关于spring - 如何通过 RestTemplate 发布字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7925053/

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