Java RestTemplate:如何发送多部分?
我需要发送请求。看起来像这样的卷曲请求:Java RestTemplate:如何发送多部分?,java,multipartform-data,resttemplate,Java,Multipartform Data,Resttemplate,我需要发送请求。看起来像这样的卷曲请求: curl --location --request POST 'http://127.0.0.1:8080/api/attachments' --form 'data=@path/to/file/6C233C5F-B60D-41CA-86ED-A43C51FD7EED.png' 如果要将其转换为IDEA http请求,应如下所示: POST http://127.0.0.1:8080/api/attachments Content-Type: mul
curl --location --request POST 'http://127.0.0.1:8080/api/attachments'
--form 'data=@path/to/file/6C233C5F-B60D-41CA-86ED-A43C51FD7EED.png'
如果要将其转换为IDEA http请求,应如下所示:
POST http://127.0.0.1:8080/api/attachments
Content-Type: multipart/form-data; boundary=WebAppBoundary
--WebAppBoundary
Content-Disposition: form-data; name="data"; filename="6C233C5F-B60D-41CA-86ED-A43C51FD7EED.png"
< path/to/file/6C233C5F-B60D-41CA-86ED-A43C51FD7EED.png
--WebAppBoundary--
POSThttp://127.0.0.1:8080/api/attachments
内容类型:多部分/表单数据;边界=WebAppBoundary
--WebAppBoundary
内容配置:表单数据;name=“数据”;filename=“6C233C5F-B60D-41CA-86ED-A43C51FD7EED.png”
好的,但是我需要用restemplate
发送它。最好使用byte[]
而不是文件路径。
请帮忙。这是我的代码片段
HttpEntity<?> entity;
HttpHeaders headers = new HttpHeaders();
Object requestBody;
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
requestBody = Base64.getDecoder().decode(base64EncodedData);
entity = new HttpEntity<>(requestBody, headers);
ResponseEntity response = getRestTemplate().exchange(
serviceURL,
HttpMethod.POST,
entity,
String.class
);
HttpEntity;
HttpHeaders=新的HttpHeaders();
对象请求体;
headers.setContentType(MediaType.APPLICATION\u OCTET\u STREAM);
requestBody=Base64.getDecoder().decode(base64EncodedData);
实体=新的HttpEntity(请求主体、标题);
ResponseEntity response=getRestTemplate().exchange(
serviceURL,
HttpMethod.POST,
实体,
String.class
);
您可以将文件内容读取到字节数组并以相同的方式发送