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

标签 java spring http web

我想向休息端点发送 POST 请求。其余端点 documentation说:

Create a node and add it as a primary child of node nodeId.

This endpoint supports both JSON and multipart/form-data (file upload).

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.

curl -utest:test -X POST host:port/alfresco/api/-default-/public/alfresco/versions/1/nodes/-my-/children -F [email protected]

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. The default is cm:content

我设法通过以下代码向此端点发送正确的请求:

@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);
}

但我有两个问题:

1 - 为什么注释行不起作用?

2 - 文档说:

You can use the name field to give an alternative name for the new file.

我没有使用"name"字段,但服务器用正确的名称保存了我的文件(="name22222"),为什么?(我想multipart/form-data 是一种简单的名称值,如果这是正确的,那么我有一个名为“filedata”的字段,其值是我的 byte 数组内容,那么文件名是如何发送的?)。如何使用字段指定文件名?

更新: 我想我找到了答案。我只需要阅读multipart/form-data!

最佳答案

也许这个示例对于了解如何使用 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);

关于java - 如何使用 Spring RestTemplate 发送多部分/表单数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61147883/

相关文章:

java - 我的代码和/或逻辑有什么问题?

java - 如何将以毫秒为单位的 java.time.LocalTime 映射为以毫秒为单位的 java.sql.Time

java - KeyListener 方法内的代码不起作用

java - 在 Android TV 中的 fragment 上创建叠加层?

java - 如何将参数传递给验证Spring Webflow的方法?

java - 测试时,Web 服务中的 Spring Autowired 不工作

php - 如何使用 PHP 为文本文件显示 'Save as' 对话框

c++ - 是否有异步C++ HTTP框架可用?

java - 可以从 Glass 发送 HTTP 请求吗?

java - GWT 的工具提示