Loading

Java - InputStream流Http客户端文件上传

场景:两个系统文件服务不同,需从另外一个系统中下载文件并上传到另外一个系统中。

代码实现

//远程服务下载文件
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);
}
posted @ 2023-10-26 11:00  IamHzc  阅读(371)  评论(0编辑  收藏  举报