场景:两个系统文件服务不同,需从另外一个系统中下载文件并上传到另外一个系统中。
代码实现
//远程服务下载文件
Response response = fileCenterService.downloadFile(fileId);
InputStream inputStream = response.body().asInputStream();
//调用接口上传到文件服务
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(uploadUrl);
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.addPart("file", new InputStreamBody(inputStream, ContentType.DEFAULT_BINARY, "file.docx"));
HttpEntity httpEntity = entityBuilder.build();
httpPost.setEntity(httpEntity);
httpPost.setHeader("Authorization", "Bearer" + token);
HttpResponse downRes = httpClient.execute(httpPost);
HttpEntity responseEntity = downRes.getEntity();
String responseBody = null;
if (responseEntity != null) {
responseBody = EntityUtils.toString(responseEntity);
}
标签:文件,entityBuilder,Java,httpPost,InputStream,responseEntity,Http
From: https://www.cnblogs.com/IamHzc/p/17788943.html