简体   繁体   English

如何使用 Spring RestTemplate 发送多部分/表单数据?

[英]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.

相关问题 如何使用restTemplate Spring-mvc发送多部分表单数据 - How to send Multipart form data with restTemplate Spring-mvc RestTemplate &amp; multipart/form-data 响应 - RestTemplate & multipart/form-data response 防止 Spring 的 RestTemplate 为 multipart/form-data 中的每个参数添加标头 - Prevent Spring's RestTemplate from adding header for each parameters in multipart/form-data Spring RestTemplate:多部分/表单数据禁止获取 403,但使用 curl - Spring RestTemplate: getting 403 forbidden for multipart/form-data but working with curl 如何处理需要在 Spring 引导中发送 JSON 和 multipart/form-data 的情况 - How can I handle a situation where I need to send JSON and multipart/form-data in Spring boot Android RestTemplate使用utf-8发布multipart / form-data - Android RestTemplate post multipart/form-data with utf-8 如何使用AngularJS $ http发送multipart / form-data - How to use AngularJS $http to send multipart/form-data java 如何将 Multipart/Form-data 发送到 rest 服务 - java how to send Multipart/Form-data to rest service 如何从 Spring WebFlux 中的 Multipart/form-data 中的 stream 文件 - How to stream file from Multipart/form-data in Spring WebFlux 如何在Spring MVC中使用multipart / form-data - how to use multipart/form-data in spring mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM