0
点赞
收藏
分享

微信扫一扫

java保存api返回的pdf流文件

蒸熟的土豆 2022-10-19 阅读 184
  1. 方法一

ResponseEntity<Resource> resource = restTemplate.postForEntity(reUrl, requestEntity , Resource.class);
InputStream inputStream = resource.getBody().getInputStream();
byte[] label = this.input2byte(inputStream);
String labelStr = Base64.getEncoder().encodeToString(label);

  1. 方法二

 HttpResponse<InputStream> response = Unirest.get(reUrl)
.basicAuth(order.getCompany().getAppUser(),order.getCompany().getAppPass())
.asBinary();
byte[] label = this.input2byte(response.getBody());
//System.out.println(res);
// byte[] label = res.getBytes(StandardCharsets.UTF_8);
String labelStr = Base64.getEncoder().encodeToString(label);

public byte[] input2byte(InputStream inStream) throws IOException {
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[100];
int rc = 0;
while ((rc = inStream.read(buff, 0, 100)) > 0) {
swapStream.write(buff, 0, rc);
}
byte[] in2b = swapStream.toByteArray();
return in2b;
}

举报

相关推荐

0 条评论