HttpClient Download File from URL

 Posted On  | Yashwant Chavan 

In this tutorial you will learn how to download file from URL using Apache HttpClient request. In this example we make a http request to server location and download the respected zip file and save it to local file system.

Maven dependency for Apache httpclient


Refer below steps to download file from URL using Apache http client.

  • Create instance of CloseableHttpClient using HttpClientBuilder.create().build(). CloseableHttpClient is an abstract class which is the base implementation of HttpClient and interface.
  • Create http GET request using HttpGet
  • Make a Request - Execute the Download file request using .execute() method.
  • HttpResponse.getEntity() returns the HttpEntity instance. Which can be sent or received with an HTTP message. Entities can be found in some requests and in responses.
  • HttpEntity.getContent() returns a content stream of the entity. Once you received InputStream from response write it into File using FileOutputStream.
  • Finally close all resources after completion of HttpClient request.
  • Verify output, zip file is created on mentioned file path.

Download File Using HttpCilent

package com.technicalkeeda.http;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

public class DownloadFileHttpCilent {

    public static void main(String[] args) throws Exception {
        DownloadFileHttpCilent client = new DownloadFileHttpCilent();;

    public void download() {
        try {
            CloseableHttpClient client = HttpClientBuilder.create().build();
            HttpGet request = new HttpGet(

            HttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();

            int responseCode = response.getStatusLine().getStatusCode();

            System.out.println("Request Url: " + request.getURI());
            System.out.println("Response Code: " + responseCode);

            InputStream is = entity.getContent();

            String filePath = "c:\\demo\\";
            FileOutputStream fos = new FileOutputStream(new File(filePath));

            int inByte;
            while ((inByte = != -1) {


            System.out.println("File Download Completed!!!");
        } catch (ClientProtocolException e) {
        } catch (UnsupportedOperationException e) {
        } catch (IOException e) {



Request Url:
Response Code: 200
File Download Completed!!!

© 2018

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0237 seconds.