上传文件到腾讯云代码理解

public Map exportFileToCloud(String filePath,String uploadPath){

    File fileRe = new File(filePath);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.DATA);
    headers.add("id","DFGGFHG-12");
    MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
    form.add("file", new FileSystemResource(fileRe));
    form.add("MediaType",fileRe.getName());
    HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
    String url = applicationProperties.getCloudIp() + uploadPath;
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<Map> entity = null;
    Map body = null;
    try {
        entity = restTemplate.postForEntity(url, files, Map.class);
        body = entity.getBody();

    }catch (Exception e){

        log.error("上传文件{}到腾讯云失败,请检查",filePath);

    }

    return body;
}

传参两个参数,1. filePath 文件存在路径。2.uploadPath 文件上传路径。HttpHeaders 就相当于是一个报文头,里面存储一些相应的提示。MultiValueMap (Map之一个Key存多个Value的MultiValueMap(一个键多个值))。RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。其中重要的一个就是try中返回的entity 是一个http响应实体,
其实深入的去看这些代码都没什么难处,基本都可以百度中查出来,加油加油。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值