Class FileProxy

java.lang.Object
net.dv8tion.jda.api.utils.FileProxy
Direct Known Subclasses:
AttachmentProxy, ImageProxy

public class FileProxy extends Object
A utility class to download files.
  • Constructor Details

  • Method Details

    • setDefaultHttpClient

      public static void setDefaultHttpClient(@Nonnull okhttp3.OkHttpClient httpClient)
      Sets the default OkHttpClient used by FileProxy and ImageProxy.
      This can still be overridden on a per-instance basis with withClient(OkHttpClient).
      Parameters:
      httpClient - The default OkHttpClient to use while making HTTP requests
      Throws:
      IllegalArgumentException - If the provided OkHttpClient is null
    • getUrl

      @Nonnull public String getUrl()
      Returns the URL that has been passed to this proxy.
      This URL is always from Discord.
      Returns:
      The URL of the file.
    • withClient

      @Nonnull public FileProxy withClient(@Nonnull okhttp3.OkHttpClient customHttpClient)
      Sets the custom OkHttpClient used by this instance, regardless of if setDefaultHttpClient(OkHttpClient) has been used or not.
      Parameters:
      customHttpClient - The custom OkHttpClient to use while making HTTP requests
      Returns:
      This proxy for chaining convenience.
      Throws:
      IllegalArgumentException - If the provided OkHttpClient is null
    • download

      Retrieves the InputStream of this file
      Returns:
      CompletableFuture which holds an InputStream, the InputStream must be closed manually
    • downloadToPath

      Downloads the data of this file, and stores it in a file with the same name as the queried file name (this would be the last segment of the URL).

      Implementation note: The file is first downloaded into a temporary file, the file is then moved to its real destination when the download is complete.

      Returns:
      CompletableFuture which holds a Path which corresponds to the location the file has been downloaded.
    • downloadToFile

      Downloads the data of this file into the specified file.

      Implementation note: The file is first downloaded into a temporary file, the file is then moved to its real destination when the download is complete.

      Parameters:
      file - The file in which to download the data
      Returns:
      CompletableFuture which holds a File, it is the same as the file passed in the parameters.
      Throws:
      IllegalArgumentException - If any of the follow checks are true
      • The target file is null
      • The parent folder of the target file does not exist
      • The target file exists and is not a regular file
      • The target file exists and is not writable
    • downloadToPath

      Downloads the data of this file into the specified file.

      Implementation note: The file is first downloaded into a temporary file, the file is then moved to its real destination when the download is complete.
      The given path can also target filesystems such as a ZIP filesystem.

      Parameters:
      path - The file in which to download the image
      Returns:
      CompletableFuture which holds a Path, it is the same as the path passed in the parameters.
      Throws:
      IllegalArgumentException - If any of the follow checks are true
      • The target path is null
      • The parent folder of the target path does not exist
      • The target path exists and is not a regular file
      • The target path exists and is not writable