My application makes a connection to an API that returns a PDF. This PDF is returned as an attachment according to the following image and when I try to save this file to any folder through my application, or an error occurs or the file is saved in white.
WhatkindofimplementationcouldIusetosolvethisproblem?
BelowisanexampleofanimplementationthatItriedbutdidnotwork.
publicstaticStringgetPdf(IntegercodigoEmpresa,IntegernumeroCupomSap,IntegernumParcela){StringinfoPdfRetornado="";
Client client = Client.create();
WebResource webResource = client.resource(ConstantesIntegrationBankApi.teste);
String auth = "tiroliro";
ClientResponse response = webResource.header("Authorization", auth).type("application/pdf").get(ClientResponse.class);
if (response.getStatus() == Response.Status.OK.getStatusCode())
{
ChunkedInputStream cis = response.getEntity(ChunkedInputStream.class);
byte[] unchunkedData = null;
byte[] buffer = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int read = -1;
try
{
while ((read = cis.read(buffer)) != -1)
{
bos.write(buffer, 0, read);
}
unchunkedData = bos.toByteArray();
bos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
salvarPdf(unchunkedData);
}
return infoPdfRetornado;