BAEL-4856 Download a binary file using OkHttp

This commit is contained in:
Iulian Manda
2021-05-31 15:51:38 +03:00
parent 050c53be0b
commit bf171adee1
5 changed files with 249 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
package com.baeldung.okhttp.download;
import okhttp3.OkHttpClient;
import java.io.FileOutputStream;
public class BinaryFileDownloadApplication {
public static void main(String[] args) {
String url = args[0];
String file = args[1];
try (BinaryFileDownloader downloader = new BinaryFileDownloader(new OkHttpClient(),
new BinaryFileWriter(new FileOutputStream(file)))) {
long downloadSize = downloader.download(url);
double downloadSizeInMB = convertToMB(downloadSize);
System.out.printf("Successfully downloaded file %s from %s. Total size %.2fMB%n", file, url, downloadSizeInMB);
} catch (Exception ex) {
System.err.printf("Could not download file %s from %s. %nError: %s%n", file, url, ex);
}
}
private static double convertToMB(double downloadSize) {
return downloadSize / (1024.0 * 1024.0);
}
}