SpringBoot请求第三方接口


/* SpringBoot请求第三方接口  */
String url="http://192.168.3.8:8011/file/openadd";
RestTemplate client = new RestTemplate();

// 设置请求头
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("multipart/form-data");
headers.setContentType(type);

// 设置请求体,注意是LinkedMultiValueMap
FileSystemResource fileSystemResource = new FileSystemResource(uploadedFile);
MultiValueMap form = new LinkedMultiValueMap<>();

UUIDFileName = UUID.randomUUID().toString().replace("-","");
form.add("file", fileSystemResource);
form.add("fileName",UUIDFileName);
form.add("type",3);
form.add("updateFlag",0);
form.add("createByName",""+ UserUtils.getUser().getName());
form.add("createBy",""+UserUtils.getUser().getId());

// 用HttpEntity封装整个请求报文
HttpEntity> files = new HttpEntity<>(form, headers);

String str = client.postForObject(url, files, String.class);


 

你可能感兴趣的:(Java)