Class FileDownloadHandler

All Implemented Interfaces:
DownloadHandler, ElementRequestHandler, Serializable

public class FileDownloadHandler extends AbstractDownloadHandler<FileDownloadHandler>
Download handler for use with a given File that will be read and written as binary data to the response.
Since:
24.8
See Also:
  • Constructor Details

    • FileDownloadHandler

      public FileDownloadHandler(File file)
      Create a download handler for given file. Url postfix will be used as file.getName().

      The downloaded file name and download URL postfix will be set to file.getName(). If you want to use a different file name, use FileDownloadHandler(File, String) instead.

      Parameters:
      file - file to download
    • FileDownloadHandler

      public FileDownloadHandler(File file, String fileNameOverride)
      Create a download handler for given file.

      The downloaded file fileNameOverride and download URL postfix will be set to fileNameOverride.

      Parameters:
      file - file to download
      fileNameOverride - used as a downloaded file name (overrides file.getName()) and also as a download request URL postfix, e.g. /VAADIN/dynamic/resource/0/5298ee8b-9686-4a5a-ae1d-b38c62767d6a/my-file.txt
  • Method Details

    • handleDownloadRequest

      public void handleDownloadRequest(DownloadEvent downloadEvent) throws IOException
      Description copied from interface: DownloadHandler
      Method that is called when the client wants to download from the url stored for this specific handler registration.
      Parameters:
      downloadEvent - download event containing the necessary data for writing the response
      Throws:
      IOException - if an IO error occurred during download
    • getUrlPostfix

      public String getUrlPostfix()
      Description copied from interface: ElementRequestHandler
      Optional URL postfix allows appending an application-controlled string, e.g. the logical name of the target file, to the end of the otherwise random-looking download URL. If defined, requests that would otherwise be routable are still rejected if the postfix is missing or invalid. Postfix changes the last segment in the resource url.
      Returns:
      String optional URL postfix, or null for "".
    • getTransferContext

      protected TransferContext getTransferContext(DownloadEvent transferEvent)
      Description copied from class: TransferProgressAwareHandler
      This method is used to get the transfer context from the transfer events (e.g. DownloadEvent).
      Overrides:
      getTransferContext in class AbstractDownloadHandler<FileDownloadHandler>
      Parameters:
      transferEvent - the transfer event
      Returns:
      the transfer context