[英]How to send multipart/form-data with Spring RestTemplate?
I want to send a POST
request to a rest endpoint.我想向 rest 端点发送
POST
请求。 the rest endpoint documentation says: rest 端点文档说:
Create a node and add it as a primary child of node nodeId.
创建一个节点并将其添加为节点 nodeId 的主要子节点。
This endpoint supports both JSON and multipart/form-data (file upload).
此端点同时支持 JSON 和 multipart/form-data(文件上传)。
Using multipart/form-data
使用多部分/表单数据
Use the filedata field to represent the content to upload, for example, the following curl command will create a node with the contents of test.txt in the test user's home folder.
使用 filedata 字段表示要上传的内容,例如,下面的 curl 命令将在测试用户的主文件夹中创建一个包含 test.txt 内容的节点。
curl -utest:test -X POST host:port/alfresco/api/-default-/public/alfresco/versions/1/nodes/-my-/children -F filedata=@test.txt
curl -utest:test -X POST 主机:port/alfresco/api/-default-/public/alfresco/versions/1/nodes/-my-/children -F filedata=@test.txt
You can use the name field to give an alternative name for the new file.
您可以使用名称字段为新文件提供替代名称。
You can use the nodeType field to create a specific type.
您可以使用 nodeType 字段来创建特定类型。 The default is cm:content
默认为 cm:content
I managed to send a correct request to this endpoint by the following code:我设法通过以下代码向该端点发送了正确的请求:
@Override
public ResponseEntity<byte[]> postNode(String nodeId, byte[] content) {
MultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<>();
ByteArrayResource contentsAsResource = new ByteArrayResource(content) {
@Override
public String getFilename() {
return "name22222";
}
};
bodyMap.add("filedata", contentsAsResource);
///bodyMap.add("filedata", content);// why this does not work??!
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(bodyMap, headers);
return restTemplate.exchange("/nodes/{nodeId}/children", HttpMethod.POST, requestEntity, byte[].class, nodeId);
}
but I have two questions:但我有两个问题:
1 - why the commented line does not work? 1 - 为什么注释行不起作用?
2 - the docs says: 2 - 文档说:
You can use the name field to give an alternative name for the new file.
您可以使用名称字段为新文件提供替代名称。
I did not use a "name"
field, but the server saved my files with the correct name(= "name22222"
), why?(I thought multipart/form-data
is a kind of a simple name-value, if this is correct then I have a field named "filedata" and its value is my byte
array content, so how the file name is send?).我没有使用
"name"
字段,但是服务器使用正确的名称(= "name22222"
)保存了我的文件,为什么?(我认为multipart/form-data
是一种简单的名称值,如果这是正确然后我有一个名为“filedata”的字段,它的值是我的byte
数组内容,那么文件名是如何发送的?)。 and how can I specify the file name with a field ?以及如何使用字段指定文件名?
update: i think i found my answers.更新:我想我找到了答案。 i just need to read about
multipart/form-data
!我只需要阅读有关
multipart/form-data
!
Perhaps this example will be useful to find out how you can upload one or more files using Spring.也许此示例有助于了解如何使用 Spring 上传一个或多个文件。
byte[] fileContent = "testFileContent".getBytes();
String filename = "testFile.xml";
MultiValueMap<String, String> fileMap = new LinkedMultiValueMap<>();
ContentDisposition contentDisposition = ContentDisposition
.builder("form-data")
.name("file")
.filename(filename)
.build();
fileMap.add(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString());
HttpEntity<byte[]> fileEntity = new HttpEntity<>(fileContent, fileMap);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", fileEntity);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
ResponseEntity<String> response = restTemplate
.postForEntity("/import/file, requestEntity, String.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.