Code ví dụ download file bằng Java (Apache HttpComponents – HttpClient)

Code ví dụ download file bằng Java (Apache HttpComponents – HttpClient)

Ở ví dụ này mình sẽ sử dụng thư viện Apache HttpComponents để gửi request download file.

(Xem lại: Ví dụ gửi http get request bằng Java (HttpClient))

Tạo Maven Project

Code ví dụ download file bằng Java (Apache HttpComponents - HttpClient)

Thư viện sử dụng:

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>fluent-hc</artifactId>
  <version>4.5.5</version>
</dependency>

Cách download file đơn giản nhất là sử dụng fluent API

public static void downloadFile1(String filePath, String urlDownload) throws ClientProtocolException, IOException {
  File fileDownload = new File(filePath);
  Response response = Request.Get(urlDownload).execute();
  response.saveContent(fileDownload);
}

Hoặc bạn cũng có thể đọc dữ liệu từ đối tượng HttpEntiy của Response và lưu ra file, bạn cũng có thể lấy các thông tin khác như kích thước file, content type từ HttpEntity.

Sử dụng fluent API

public static void downloadFile2(String filePath, String urlDownload) throws ClientProtocolException, IOException {
  File fileDownload = new File(filePath);
  Response response = Request.Get(urlDownload).execute();
  HttpResponse httpResponse = response.returnResponse();
  InputStream is = httpResponse.getEntity().getContent();
  FileOutputStream fos = new FileOutputStream(fileDownload);
  int inByte;
  while ((inByte = is.read()) != -1) {
    fos.write(inByte);
  }
  is.close();
  fos.close();
}

Sử dụng HttpClient

public static void downloadFile3(String filePath, String urlDownload) throws ClientProtocolException, IOException {
  File fileDownload = new File(filePath);
  HttpGet httpGet = new HttpGet(urlDownload);
  HttpClient client = HttpClients.createDefault();
  HttpResponse httpResponse = client.execute(httpGet);

  InputStream is = httpResponse.getEntity().getContent();
  FileOutputStream fos = new FileOutputStream(fileDownload);
  int inByte;
  while ((inByte = is.read()) != -1) {
    fos.write(inByte);
  }
  is.close();
  fos.close();
}

Demo:

package stackjava.com.httpcomponentsdownload.demo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;

public class DemoDownload {

  public static void downloadFile1(String filePath, String urlDownload) throws ClientProtocolException, IOException {
    File fileDownload = new File(filePath);
    Response response = Request.Get(urlDownload).execute();
    response.saveContent(fileDownload);
  }

  public static void downloadFile2(String filePath, String urlDownload) throws ClientProtocolException, IOException {
    File fileDownload = new File(filePath);
    Response response = Request.Get(urlDownload).execute();
    HttpResponse httpResponse = response.returnResponse();
    InputStream is = httpResponse.getEntity().getContent();
    FileOutputStream fos = new FileOutputStream(fileDownload);
    int inByte;
    while ((inByte = is.read()) != -1) {
      fos.write(inByte);
    }
    is.close();
    fos.close();

  }

  public static void downloadFile3(String filePath, String urlDownload) throws ClientProtocolException, IOException {
    File fileDownload = new File(filePath);
    HttpGet httpGet = new HttpGet(urlDownload);
    HttpClient client = HttpClients.createDefault();
    HttpResponse httpResponse = client.execute(httpGet);

    InputStream is = httpResponse.getEntity().getContent();
    FileOutputStream fos = new FileOutputStream(fileDownload);
    int inByte;
    while ((inByte = is.read()) != -1) {
      fos.write(inByte);
    }
    is.close();
    fos.close();
  }

  public static void main(String[] args) throws ClientProtocolException, IOException {
    File folderDownload = new File(System.getProperty("user.home") + "/Downloads");
    if (!folderDownload.exists()) {
      folderDownload.mkdirs();
    }
    String urlDownload = "https://stackjava.com/wp-content/uploads/2018/05/SpringBootDownloadFile.zip";
    downloadFile1(folderDownload+"/file1.zip", urlDownload);
    downloadFile2(folderDownload+"/file2.zip", urlDownload);
    downloadFile3(folderDownload+"/file3.zip", urlDownload);
  }
}

Mình có upload 1 file lên url “https://stackjava.com/wp-content/uploads/2018/05/SpringBootDownloadFile.zip” bây giờ mình sẽ gửi request đến đường link đó để download file.

Bạn có thể test đường link bằng cách truy cập url này bằng trình duyệt.

Các file download về sẽ được lưu ở folder downloads

Code ví dụ download file bằng Java (Apache HttpComponents - HttpClient)

Code ví dụ download file bằng Java (Apache HttpComponents – HttpClient) stackjava.com

Okay, Done!

Download code ví dụ trên tại đây.

 

References:

https://hc.apache.org/

https://hc.apache.org/httpcomponents-core-ga/tutorial/html/index.html

https://hc.apache.org/httpcomponents-client-ga/tutorial/html/

stackjava.com