How to Build HttpClient In Java to Download File

  • Post last modified:December 15, 2022
  • Reading time:2 mins read

Introduction

  • In this article, we will build HTTP Client that will download files from the internet.
  • We will download image files and pdf files for this demo but we can extend it to any type of file.
  • FYI, this code requires Java 11 since we will use HttpClient which was added in Java 11.

Building Request Object

  • At first, we will build HTTP GET request. HttpRequest follows the builder pattern.
private static HttpRequest getHttpRequest(String uri) {
        HttpRequest request = HttpRequest
                .newBuilder()
                .uri(URI.create(uri))
                .GET()
                .build();
        return request;
}

Building HttpClient Object

  • After that, we will build httpclient and execute to get the response. Since the response consists of files we need to pass bodyhandlers as of InputStream to read the file.
private static HttpResponse<InputStream> getResponse(HttpRequest request) throws IOException, InterruptedException {
        return HttpClient
                .newHttpClient()
                .send(request, HttpResponse.BodyHandlers.ofInputStream());
}

Writing to Local File

  • Writing to a local file is easy, we can write using FileOutputStream. Here we are getting content as inputstream.
private static void WriteLocal(InputStream body, String destinationFIle) throws IOException {
        FileOutputStream fos = new FileOutputStream(destinationFIle);
        fos.write(body.readAllBytes());
        fos.close();
}

Download Image Client

  • We are downloading endpoint “ https://miro.medium.com/max/864/1*EDEtjTqyDwAvqdAiMIWXww.png”
  • Since we are downloading images, the response body contains inputstream.
  • Once we have inputstream, we can write to the local disk.
private static void downloadImage(String imageName) throws IOException, InterruptedException {
        HttpRequest request = getHttpRequest(imageName);

        HttpResponse<InputStream> response = getResponse(request);

        System.out.println("status code: "+response.statusCode());
        InputStream body = response.body();

        WriteLocal(body, "image.png");
 }

Download PDF Client

  • Similarly, we can also download PDF files, Code will look exactly the same as of Image file.
 private static void downloadPDF(String pdfFile) throws IOException, InterruptedException {
        HttpRequest request = getHttpRequest(pdfFile);
        HttpResponse<InputStream> response = getResponse(request);

        System.out.println("status code: "+response.statusCode());
        InputStream body = response.body();

        WriteLocal(body, "sample.pdf");
}

Conclusion

  • In this article, we used Java 11 HttpClient to download images and pdf file. BodyHandler provides different ways to handle response body, and in the case of files we can use InputStream
  • We can download any type of file using this approach.

Bonus Tip

Leave a Reply