lem*_*mon 10 spring bytearray resttemplate
目标:使用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);
Run Code Online (Sandbox Code Playgroud)
还有其他选择吗?POST一个包含base64编码的byte []数组的JSON是一个有效的替代方案吗?
Ale*_*tet 14
是的,我想是这样的
如果图像是您的有效负载,如果您想调整标题,可以这样发布:
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);
Run Code Online (Sandbox Code Playgroud)
除此以外 :
InputStream in = new ClassPathResource("myFile.jpg").getInputStream();
HttpEntity<byte[]> entity = new HttpEntity<>(IOUtils.toByteArray(in));
template.postForEntity("http://example.com/myFileUpload", entity, String.class);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17535 次 |
| 最近记录: |