Class FileDownloader
java.lang.Object
de.gustavblass.commons.FileDownloader
A utility class that downloads files from the internet.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
Whether to send the Do-Not-Track header with HTTP requests.private boolean
Whether to send the Global-Privacy-Control header with HTTP requests.private static final org.apache.logging.log4j.Logger
private @Nullable String
The user-agent string to be sent with HTTP requests. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Disables that thedoNotTrack
header is sent with any HTTP requests.void
Disables that theglobalPrivacyControl
header is sent with any HTTP requests.void
Retrieves a file of arbitrary format from the server available at the given web address to the specified destination.void
Enables that thedoNotTrack
header is sent with any HTTP requests.void
Enables that theglobalPrivacyControl
header is sent with any HTTP requests.Retrieves the customuserAgent
string to be sent with HTTP requests.private static Optional
<byte[]> Extracts the content of the given local file.void
Deletes the customuserAgent
string previously set.void
setUserAgent
(@NonNull String userAgent) Updates theuserAgent
string to be sent with HTTP requests.private static void
Saves the content to the given local file.
-
Field Details
-
LOG
private static final org.apache.logging.log4j.Logger LOG -
userAgent
The user-agent string to be sent with HTTP requests. -
doNotTrack
private boolean doNotTrackWhether to send the Do-Not-Track header with HTTP requests. -
globalPrivacyControl
private boolean globalPrivacyControlWhether to send the Global-Privacy-Control header with HTTP requests.
-
-
Constructor Details
-
FileDownloader
public FileDownloader()
-
-
Method Details
-
setUserAgent
public void setUserAgent(@NonNull @NonNull String userAgent) throws de.gustavblass.commons.exceptions.IllegalArgumentException Updates theuserAgent
string to be sent with HTTP requests.- Parameters:
userAgent
- The new user agent string. Must not be empty or blank.- Throws:
de.gustavblass.commons.exceptions.IllegalArgumentException
- If the user agent string is empty or blank.
-
resetUserAgent
public void resetUserAgent()Deletes the customuserAgent
string previously set. -
getUserAgent
-
enableDoNotTrack
public void enableDoNotTrack()Enables that thedoNotTrack
header is sent with any HTTP requests. -
disableDoNotTrack
public void disableDoNotTrack()Disables that thedoNotTrack
header is sent with any HTTP requests. -
enableGlobalPrivacyControl
public void enableGlobalPrivacyControl()Enables that theglobalPrivacyControl
header is sent with any HTTP requests. -
disableGlobalPrivacyControl
public void disableGlobalPrivacyControl()Disables that theglobalPrivacyControl
header is sent with any HTTP requests. -
download
public void download(@NonNull @NonNull URL url, @NonNull @NonNull File target) throws IOException, de.gustavblass.commons.exceptions.IllegalArgumentException, InterruptedException Retrieves a file of arbitrary format from the server available at the given web address to the specified destination.
Sends the custom
userAgent
string if set. Otherwise, the default user agent of the Java HTTP client is used.Sends the Do-Not-Track signal if
doNotTrack
is set totrue
. Similarly, sends the Global-Privacy-Control signal ifglobalPrivacyControl
is set totrue
.- Parameters:
url
- The publicly available web 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:
de.gustavblass.commons.exceptions.IllegalArgumentException
- If the target file exists but is a directory or if the path to the target file is invalid.IOException
- If an error occurs while downloading the file.InterruptedException
- If theThread
is interrupted while waiting for the download to finish.
-
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.
-