RestTemplate 将文件作为字节从一个控制器发送到另一个 [英] RestTemplate send file as bytes from one controller to another

查看:178
本文介绍了RestTemplate 将文件作为字节从一个控制器发送到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们在第三方服务上有一个单一的控制器,它接受多部分文件,其代码类似于(假设它在 localhost:9090 上运行)

assume we have a one controller on third party service which accepts multipart files and its code is like (assume it's running on localhost:9090)

@RequestMapping("/file")
@RestController
public class FileController {

    @RequestMapping(value = "/load", method = RequestMethod.POST)
    public String getFile(@RequestPart("file") MultipartFile file){
        return file.getName();
    }

}

问题是:如何在我的控制器中编写正确的代码,使用 RestTemplate,调用第三方服务,文件在正文中?

The question is: How write a correct code in my controller, with RestTemplate, that calls the third party service, with file in body?

一些不起作用的例子:

第一个:

@RequestMapping("/file")
@RestController
public class FileSendController {

    private RestTemplate restTemplate = new RestTemplate();

    @RequestMapping(value = "/send", method = RequestMethod.POST)
    public ResponseEntity<?> sendFile(@RequestPart MultipartFile file) 
    throws IOException {
    String url = "http://localhost:9090/file/load";
    return restTemplate.postForEntity(url, file.getBytes(), 
    ResponseEntity.class);
    }
}

第二个:

@RequestMapping("/file")
@RestController
public class FileSendController {

    private RestTemplate restTemplate = new RestTemplate();

    @RequestMapping(value = "/send", method = RequestMethod.POST)
    public ResponseEntity<?> sendFile(@RequestPart MultipartFile file) 
    throws IOException {
        String url = "http://localhost:9090/file/load";
        byte[] bytes = file.getBytes();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<byte[]> entity = new HttpEntity<>(bytes, headers);
        return restTemplate.exchange(url, HttpMethod.POST, 
        entity,ResponseEntity.class);
    }
}

一个限制:我应该从内存中加载文件,所以它迫使我使用 byte[]

One restriction: i should load files from memory, so it forces me to use byte[]

所有这些示例在带有消息的第三方服务上抛出 500:org.springframework.web.multipart.MultipartException:当前请求不是多部分请求.

All of this examples throw 500 on third party service with message: org.springframework.web.multipart.MultipartException: Current request is not a multipart request.

感谢您的建议.

推荐答案

试试这个:

MultiValueMap<String, Object> data = new LinkedMultiValueMap<String, Object>();
ByteArrayResource resource = new ByteArrayResource(file.getBytes()) {
        @Override
        public String getFilename() {
            return file.getName();
        }
};
data.add("file", resource);
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(data, requestHeaders);

final ResponseEntity<Response<ImportDto>> responseEntity = restTemplate.exchange(url, 
                    HttpMethod.POST, requestEntity, new ParameterizedTypeReference<Response<ResponseDto>>(){});

这篇关于RestTemplate 将文件作为字节从一个控制器发送到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆