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响应实体,
其实深入的去看这些代码都没什么难处,基本都可以百度中查出来,加油加油。