java保存api返回的pdf流文件

阅读 184

2022-10-19

  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)

0 0 举报