英文:
Send Multipart form-data resttemplate?
问题
以下是代码部分的中文翻译:
我正在尝试使用RestTemplate发送多部分表单数据,但它不起作用,似乎数据没有到达服务器。
public ResponseEntity<Map> postImage(URI postURL, String base64) {
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic basicAuthString");
headers.add("Content-Type","multipart/form-data");
base64 = "base64String";
MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
form.add("$myPicture", base64);
RestTemplate restTemplate = new RestTemplate();
FormHttpMessageConverter converter = new FormHttpMessageConverter();
// converter.setCharset(Charset.forName("UTF-8"));
restTemplate.getMessageConverters().add(converter);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(form, headers);
ResponseEntity<Map> responseObject = restTemplate.postForEntity(postURL, requestEntity, Map.class);
return responseObject;
}
请注意,这只是代码部分的翻译。如果您有其他问题或需要进一步帮助,请随时提出。
英文:
I am trying to send the multipart-form data using resttemplate, but it is not working and seems data it is not reaching on server.
public ResponseEntity<Map> postImage(URI postURL, String base64) {
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic basicAuthString");
headers.add("Content-Type","multipart/form-data");
base64 = "base64String";
MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
form.add("$myPicture", base64);
RestTemplate restTemplate = new RestTemplate();
FormHttpMessageConverter converter = new FormHttpMessageConverter();
// converter.setCharset(Charset.forName("UTF-8"));
restTemplate.getMessageConverters().add(converter);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(form, headers);
ResponseEntity<Map> responseObject = restTemplate.postForEntity(postURL, requestEntity, Map.class);
return responseObject;
}
But same API is working as expected using curl.
curl -i -X POST -H "Content-Type:multipart/form-data" ^
-H "Authorization: Basic basicAuth" ^
-F $myPicture = "base64String" ^
http://{{host}}:{{port}}/WebSDK/entity?q=entity={{entityGUID}},Picture=$myPicture
I tried various ways to get it worked but was unable to find out any resolution from this if possible could someone please guide me here...
Thanks in advance !!
答案1
得分: 0
为了解决这种情况,我需要将我的Base64编码的字符串提供为InputStream。
class MultipartInputStreamFileResource extends InputStreamResource {
private final String filename;
MultipartInputStreamFileResource(InputStream inputStream, String filename) {
super(inputStream);
this.filename = filename;
}
@Override
public String getFilename() {
return this.filename;
}
@Override
public long contentLength() throws IOException {
return -1; // 我们通常不希望将整个流读入内存...
}
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.setContentLength(requestBody.length());
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("$myPicture", new MultipartInputStreamFileResource(IOUtils.toInputStream(requestBody), filename));
HttpEntity<MultiValueMap<String,Object>> requestEntity = new HttpEntity<>(body, headers);
将此InputStreamResource实例提供到请求正文中有助于解决我的问题。
谢谢。
英文:
To Fix this scenario I was in need to provide my base64 Encoded String as InputStream.
class MultipartInputStreamFileResource extends InputStreamResource {
private final String filename;
MultipartInputStreamFileResource(InputStream inputStream, String filename) {
super(inputStream);
this.filename = filename;
}
@Override
public String getFilename() {
return this.filename;
}
@Override
public long contentLength() throws IOException {
return -1; // we do not want to generally read the whole stream into memory ...
}
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.setContentLength(requestBody.length());
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("$myPicture", new MultipartInputStreamFileResource(IOUtils.toInputStream(requestBody), filename));
HttpEntity<MultiValueMap<String,Object>> requestEntity = new HttpEntity<>(body, headers);
providing this InputStreamResource instance in the request body helped to fix my issue.
Thanks .
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论