【问题标题】:Sending a file containing json value using RestTemplate使用 RestTemplate 发送包含 json 值的文件
【发布时间】:2018-07-26 10:19:29
【问题描述】:

我想使用 RestTemplate 发送一个文件(服务器只接受 multipart/form-data)。但是,在客户端,我只有一个 JSON 对象的字节数组。我不想将其转换为创建文件的文件,因为它需要磁盘空间。我可以将此数据发送到服务器吗?

【问题讨论】:

  • 您想通过网络发送 json 数据,或者您希望将 json 数据放入文件中并希望在服务器上发送该文件。?
  • 服务器接受一个应该有 json 数据的文件。从我的客户端,我想使用该服务。但是,我在客户端没有包含 json 数据的文件。我确实有 json 对象。现在,我怎样才能将它发送到服务器?同样,服务器只接受应该包含 Json 数据的文件

标签: java spring resttemplate spring-rest


【解决方案1】:

你可以试试这个

public void uploadDocument(byte[] fileContents, final String filename) {
RestTemplate restTemplate = new RestTemplate();
String fooResourceUrl = "http://localhost:8080/spring-rest/foos"; // Dummy URL.
MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();

map.add("name", filename);
map.add("filename", filename);

// Here we 
ByteArrayResource contentsAsResource = new ByteArrayResource(fileContents) {
    @Override
    public String getFilename() {
        return filename; // Filename has to be returned in order to be able to post.
    }
};

map.add("file", contentsAsResource);

// Now you can send your file along.
String result = restTemplate.postForObject(fooResourceUrl, map, String.class);

// Proceed as normal with your results.
}

【讨论】:

  • 已经试过了。这将使文件内容为 application/octate-stream,但内容应为 application/json。当我从 POSTMAN 发送 json 文件时,服务器代码工作正常。
  • 能否请您发布您的服务器代码,我可以看看!
【解决方案2】:

这应该可以满足你的需求:

  1. 首先,创建一个restTemplate的实例,我一般是让spring boot用@Autowired自动注入一个:

    @Autowired
    RestTemplate restTemplate;
    
  2. 现在,拥有您的字节数组 - 我将调用 jsonAsByteArray 作为示例 - 并假设 fileName 作为文件名,将目标服务器的 URL 分配给字符串 - 我将调用 url以下示例-;此时,您可以这样进行:

    String requestJson = new String(jsonAsByteArray);
    
    MultiValueMap<String, Object> multipartRequest = new LinkedMultiValueMap<>();
    
    HttpHeaders jsonHeaders = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    
    HttpEntity<String> entity = new HttpEntity<String>(requestJson,jsonHeaders);
    
    multipartRequest.add("file", entity);
    multipartRequest.add("fileName", fileName);
    
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
    requestHeaders.setAccept(Arrays.asList((new MediaType[] {MediaType.MULTIPART_FORM_DATA})));
    
    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multipartRequest, requestHeaders);
    
    ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, multipartRequest, String.class);
    
  3. 检查响应中的结果;您可以像这样在控制台上简单地打印它:

    System.out.println(out.getBody());
    System.out.println(out.getStatusCode());
    

编辑:我已根据您最后的 cmets 修改了原始答案。基本上,您必须将包含 json 的 http 实体封装到另一个更大的包含多部分请求的 http 实体中。 全部取自POSTing multipart requests with RestTemplate

【讨论】:

  • 正如我提到的,服务器接受多部分/表单数据。意思是服务器期望具有 json 数据的文件
  • 哎呀,我现在正在从您的后一条评论中阅读它。将相应地重新编辑我的答案,给我 1 分钟..
猜你喜欢
  • 2020-06-10
  • 2020-12-27
  • 1970-01-01
  • 1970-01-01
  • 2012-12-28
  • 2021-05-03
  • 1970-01-01
  • 2014-12-27
  • 2014-05-19
相关资源
最近更新 更多