发送多部分表单数据 resttemplate?

huangapple go评论62阅读模式
英文:

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&lt;Map&gt; postImage(URI postURL, String base64) {
    HttpHeaders headers = new HttpHeaders();
    headers.add(&quot;Authorization&quot;, &quot;Basic basicAuthString&quot;);
    headers.add(&quot;Content-Type&quot;,&quot;multipart/form-data&quot;);
    base64 =  &quot;base64String&quot;;

    MultiValueMap&lt;String, Object&gt; form = new LinkedMultiValueMap&lt;&gt;();
    form.add(&quot;$myPicture&quot;, base64);

    RestTemplate restTemplate = new RestTemplate();
    FormHttpMessageConverter converter = new FormHttpMessageConverter();
   // converter.setCharset(Charset.forName(&quot;UTF-8&quot;));
    restTemplate.getMessageConverters().add(converter);

    HttpEntity&lt;MultiValueMap&lt;String, Object&gt;&gt; requestEntity = new HttpEntity&lt;&gt;(form, headers);
    ResponseEntity&lt;Map&gt; responseObject = restTemplate.postForEntity(postURL, requestEntity, Map.class);
    return responseObject;
}

But same API is working as expected using curl.

 curl -i -X POST -H &quot;Content-Type:multipart/form-data&quot; ^
 -H &quot;Authorization: Basic basicAuth&quot; ^
 -F $myPicture = &quot;base64String&quot; ^
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&lt;String, Object&gt; body = new LinkedMultiValueMap&lt;&gt;();
body.add(&quot;$myPicture&quot;, new MultipartInputStreamFileResource(IOUtils.toInputStream(requestBody), filename));
HttpEntity&lt;MultiValueMap&lt;String,Object&gt;&gt; requestEntity = new HttpEntity&lt;&gt;(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&lt;String, Object&gt; body = new LinkedMultiValueMap&lt;&gt;();
    body.add(&quot;$myPicture&quot;, new MultipartInputStreamFileResource(IOUtils.toInputStream(requestBody), filename));
    HttpEntity&lt;MultiValueMap&lt;String,Object&gt;&gt; requestEntity = new HttpEntity&lt;&gt;(body, headers);

providing this InputStreamResource instance in the request body helped to fix my issue.

Thanks .

huangapple
  • 本文由 发表于 2023年2月26日 20:48:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定