java.lang.Object
page.codeberg.friedolyn.util.FileDownloader
A utility class that downloads files from the internet.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Retrieves a file of arbitrary format from the server available at the given web address to the specified destination.static void
download
(@NonNull URL url, @NonNull File target, @NonNull String userAgent, boolean doNotTrack, boolean globalPrivacyControl) Retrieves a file of arbitrary format from the server available at the given web address to the specified destination.private static Optional
<byte[]> Extracts the content of the given local file.private static void
Saves the content to the given local file.
-
Constructor Details
-
FileDownloader
public FileDownloader()
-
-
Method Details
-
download
public static void download(@NonNull @NonNull URL url, @NonNull @NonNull File target) throws RuntimeException, IOException Retrieves a file of arbitrary format from the server available at the given web address to the specified destination. UsesFriedolyn
on thedefault platform
as the user agent. Will not send the Do Not Track or Global Privacy Control header with the HTTP request.- Parameters:
url
- The publicly availableweb address
of the file to be downloaded. Must not require authentication, JavaScript execution, user interaction, cookies or a browser engine.target
- The file to which the downloaded content will be written. Must be a file, not a directory.- Throws:
RuntimeException
- If the target file is not a file or if an error occurs while creating the HTTP client or the HTTP request.IOException
- If an error occurs while downloading the file.
-
download
public static void download(@NonNull @NonNull URL url, @NonNull @NonNull File target, @NonNull @NonNull String userAgent, boolean doNotTrack, boolean globalPrivacyControl) throws RuntimeException, IOException Retrieves a file of arbitrary format from the server available at the given web address to the specified destination.- Parameters:
url
- The publicly availableweb address
of the file to be downloaded. Must not require authentication, JavaScript execution, user interaction, cookies or a browser engine.target
- The file to which the downloaded content will be written. Must be a file, not a directory.userAgent
- The user agent string to be sent with the HTTP request.doNotTrack
- Whether to send the Do Not Track header with the HTTP request. No header will be sent if set tofalse
.globalPrivacyControl
- Whether to send the Global Privacy Control header with the HTTP request. No header will be sent if set tofalse
.- Throws:
RuntimeException
- If the target file is not a file or if an error occurs while creating the HTTP client or the HTTP request.IOException
- If an error occurs while downloading the file.
-
readFile
Extracts the content of the given local file.- Parameters:
file
- The file to be read. May be of any format, i.e. plain text (TXT, JSON, CSV etc.) or binary (PNG, PDF etc.)- Returns:
- The content of the file. Empty if the file does not exist or an error occurs while reading it.
-
writeFile
Saves the content to the given local file.- Parameters:
content
- The data to be written to the file. May be of any format, i.e. plain text (TXT, JSON, CSV etc.). Will not be modified.target
- The file to which the given data will be written.
-