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

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>

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 java.io.Closeable 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 java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

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();
        client.download();
    }

    public void download() {
        try {
            CloseableHttpClient client = HttpClientBuilder.create().build();
            HttpGet request = new HttpGet(
                "http://www.technicalkeeda.com/download/mongodb-distinct-example-using-spring.zip");

            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\\file.zip";
            FileOutputStream fos = new FileOutputStream(new File(filePath));

            int inByte;
            while ((inByte = is.read()) != -1) {
                fos.write(inByte);
            }

            is.close();
            fos.close();

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

}

output

Request Url: http://www.technicalkeeda.com/download/mongodb-distinct-example-using-spring.zip
Response Code: 200
File Download Completed!!!


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0328 seconds.