import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
public static void main(String[] args) {
final String filePath = “/Users/zhaohao/IdeaProjects/standard-api/standard-service/src/main/resources/jpg/baidu.jpg”;
final String url = “https://www.baidu.com/files/url”;
RestTemplate restTemplate = new RestTemplate();
//设置请求头
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("multipart/form-data");
headers.setContentType(type);
headers.set("access_token","85aea5d7-b5e8-446b-a084-441074789bf4");
//设置请求体,注意是LinkedMultiValueMap
FileSystemResource fileSystemResource = new FileSystemResource(filePath);
MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
form.add("file", fileSystemResource);
HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
String s = restTemplate.postForObject(url, files, String.class);
System.out.println(s);
}