Java | Spring boot | Download a PDF file from a URL in the

Pasan Manohara
2 min readAug 24, 2023

Here is a Spring boot service written in Java for downloading a PDF file available on a public URL.

<a href=”https://www.freepik.com/free-photo/storage-data-information-download-concept_16436890.htm#query=pdf%20download&position=3&from_view=search&track=ais">Image by rawpixel.com</a> on Freepik
Image by Freepik
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

@Service
public class PdfFileDownloadService {
@Async
public void load(String url){
try {
URL pdfUrl = new URL(url);
URLConnection urlConnection = pdfUrl.openConnection();
if (!urlConnection.getContentType().equalsIgnoreCase("application/pdf")){
return;
}

FileOutputStream outputStream = new FileOutputStream("E:\\Downloads\output.pdf");
byte[] byteArray = new byte[1024]; // amount of bytes reading from input stream at a given time
int readLength;

InputStream inputStream = pdfUrl.openStream();
while ((readLength = inputStream.read(byteArray)) > 0){
outputStream.write(byteArray, 0, readLength);
}

outputStream.flush();
outputStream.close();
inputStream.close();
}
catch (FileNotFoundException e) {
// File Not Found
}
catch (MalformedURLException e) {
// URL Invalid
}
catch (IOException e) {
// I/O
}
catch (Exception e) {
// Exception
}
}
}

Code Explanation

(Chat GPT)

The @Service annotation from the Spring framework indicates that this class is a Spring-managed service.

load() method: This is the main method of the service responsible for loading a PDF file from a given URL and saving it to the local filesystem. It takes one parameter: url (the URL of the PDF file). The method is annotated with @Async, which indicates that it will be executed asynchronously.

Inside the method:

  • The URL is transformed into a URL object (pdfUrl).
  • A connection to the URL is established using URLConnection.
  • The content type of the resource is checked to ensure it’s a PDF file.
  • If the content type is not a PDF, the method returns.
  • A FileOutputStream is created to write the downloaded content to a file.
  • The PDF content is read from the URL’s input stream and written to the output stream.

This code demonstrates a service responsible for downloading PDF files from URLs. It uses Spring’s asynchronous processing for potentially better performance and responsiveness.

--

--