码迷,mamicode.com
首页 > 其他好文 > 详细

使用RestTemplate发送multipart/form-data格式的数据

时间:2017-11-01 11:07:17      阅读:566      评论:0      收藏:0      [点我收藏+]

标签:...   for   请求头   使用   temp   lin   multi   ons   template   

现有业务场景需要使用RestTemplate发送一个post请求,请求格式为multipart/form-data的,可以使用一下方法

public Object sendRequest(Object obj) {
        RestTemplate restTemplate = new RestTemplate();

        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(
                popHeaders(obj), headers);
         //发送请求,设置请求返回数据格式为String
        ResponseEntity<String> responseEntity = restTemplate.postForEntity("http:xxx.xx", request, String.class);
                
}
//组装请求体
protected MultiValueMap<String, String> popHeaders(Object obj) {
        SubmitOrderDTO submit = (SubmitOrderDTO) obj;
        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();

        map.add("order_id", submit.getOrderId());
        map.add("userName",submit.getUserName());
        //.....
        return map;
}

 

使用RestTemplate发送multipart/form-data格式的数据

标签:...   for   请求头   使用   temp   lin   multi   ons   template   

原文地址:http://www.cnblogs.com/wangzun/p/7765387.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!