restTemplate发送post请求时修改请求头和上传文件

1.restTemplate发送post请求时修改请求头

@PostMapping("/buyWater")
    public ObjectResult<String> buyWater(@RequestBody Food food) throws Exception {
        String url = "https://192.168.98.172/ConvertService.ashx";
        String downUrl = "http://192.168.98.172/cache/files/1548230402889_635/output.docx/output.docx?md5=DzQ_apBwhgxzjAmlGsSi-w==&expires=1548231320&disposition=attachment&ooname=output.docx";
        ConvertObject convertObject = new ConvertObject(false,"docx","1548230402889","txt","wang_min",downUrl);
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        String json = JsonUtils.toJson(convertObject);
        HttpEntity<String> formEntity = new HttpEntity<String>(json, headers);
        Object result = restTemplate.postForObject(url, formEntity, Object.class);
        return ObjectResult.success(result.toString());
    }

2.上传文件

@GetMapping("/uploadPhoto")
    public ObjectResult<String> uploadPhoto() throws Exception {
        File file = new File("F:/pic/test.jpg");
        FileSystemResource resource = new FileSystemResource(file);
        String url = "http://localhost:8080/user/uploadPhoto";
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("file", resource);
        param.add("name", "我的头像");
        param.add("size", "12M");
        restTemplate.postForObject(url, param, Object.class);
        return ObjectResult.success("success");
    }
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值