当前位置:   article > 正文

Java 通过使用 RestTemplate 传输上传字节流文件_resttemplate 发送 byte

resttemplate 发送 byte
// 传递附件给XX系统
public void uploadMaterial(String token,byte[] body) throws IOException {
    token = "eyJhbMGFlMjFmOTczZjAwYWVhMDM5IiwidWlkIjoiMSJ9.e629165c7430af605853cb1c5d7d66b5565cbb7124c0bff1f1c65d762e3a3671";
    String url = "http://127.0.0.1/v5/meeting/user/uploadstmp";
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.valueOf(MediaType.MULTIPART_FORM_DATA_VALUE));
    httpHeaders.add("token",token);
    //httpHeaders.add("Content-Type",  "text/html;charset=utf-8");
    RestTemplate restTemplate = new RestTemplate();
// 由于我们传递的是byte[]数组,所以这里用的是ByteArrayResource对象
    /*ByteArrayResource byteArrayResource = new ByteArrayResource(body) {
        @Override
        public String getFilename() {
            return "file";
        }
    };*/
    ByteArrayResource byteArrayResource = new ByteArrayResource(body) {
        @Override
        public String getFilename() {
            //重写文件名称
            return "aa.docx";
        }
    };
    httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
    MultiValueMap multiValueMap = new LinkedMultiValueMap<>();
    multiValueMap.add("m_id", "24");
    multiValueMap.add("u_id", "101119");
    multiValueMap.add("file", byteArrayResource);
    ResponseEntity<String> result = null;
    HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(multiValueMap, httpHeaders);
    result = restTemplate.exchange(url, HttpMethod.POST, httpEntity,String.class);
    //responseEntity.getBody();
}
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/302816
推荐阅读
相关标签
  

闽ICP备14008679号