public Result uploadMaterial(String token) throws IOException {
String url = String.format(Constant.UPLOAD_MATERIAL_URL_POST, douyinProperties.getAppId(), token);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.valueOf(MediaType.MULTIPART_FORM_DATA_VALUE));
File file = new File("/home/user/Downloads/carl.jpg");
byte[] bytes = new byte[(int) file.length()];
FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.read(bytes);
fileInputStream.close();
ByteArrayResource byteArrayResource = new ByteArrayResource(bytes) {
@Override
public String getFilename() {
return "material_file";
}
};
MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
multiValueMap.add("material_type", 7);
multiValueMap.add("material_file", byteArrayResource);
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(multiValueMap, httpHeaders);
ResponseEntity<Result> responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, Result.class);
return responseEntity.getBody();
}
|