Blog

Back to blog posts

[Resolved] Issue downloading PDF with Java

Published Apr 08, 2020

We’re not java experts here, but we have gotten some reports from customers who run into issues after generating the PDF file – they want to download that file and save it locally. Never faced any issues with python or c#, but Java seems to have some problems. The below code is example of how to download the PDF file with Java without any problems.

 

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
public void downloadFileUtil(String pdfUrlToDownload) {
HttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(pdfUrlToDownload);
HttpResponse response;
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
FileOutputStream outstream = new FileOutputStream("/storage/directory/filename.pdf");
entity.writeTo(outstream);
}