www.dextsolution.com
DEXTUPLOAD
JK
menu toggleReference > dextuploadjk > support > common > FileDownload

dextuploadjk.support.common
Class FileDownload

Minimum version
1.0.0
Minimum environment
Java 17
Description

FileDownload is a class that provides the ability to download data from physical files and memory to a file format.

The FileDownload class is used in a Servlet/JSP environment.

# Downloading from a file

FileDownload downloader = new FileDownload();

// Set options
FileDownloadOption option = new FileDownloadOption();
option.setFilename("sea.png");

// Download from a file
downloader.download(request, response, file, option);
# Download from data (stream download)

FileDownload downloader = new FileDownload();

// Set options
FileDownloadOption option = new FileDownloadOption();
option.setFilename("profile.txt");

// Download from an input stream
downloader.download(request, response, inputStream, option);
Constructor
  • Create an object of class FileDownload.
  • Signatures

    public FileDownload()
Methods

isAllowingWeakRange

  • Returns whether to perform a partial content download using only the Range request header.

    (A partial content download is one that returns only a portion of the data as response data.)

  • Signatures

    public boolean isAllowingWeakRange()
  • Return

    true, false

setAllowingWeakRange

  • Sets whether to perform a Partial Content download with only the Range request header.

    (A partial content download is one that returns only a portion of the data as response data.)

  • Signatures

    public void setAllowingWeakRange(boolean allowingWeakRange)
  • Parameters

    Name Type Description
    allowingWeakRange boolean If true, only the Range header is processed; if false, the If-Range header is checked.

isRemoveAfterDownloading

  • Returns whether to delete the target file after downloading the file.

  • Signatures

    public boolean isRemoveAfterDownloading()
  • Return

    true, false

setRemoveAfterDownloading

  • Sets whether to delete the target file after downloading the file.

    Deleting the file will only work if the entire file is downloaded as an attachment, and deleting the file may fail if the target file is preempted by another system.

  • Signatures

    public void setRemoveAfterDownloading(boolean removeAfterDownloading)
  • Parameters

    Name Type Description
    removeAfterDownloading boolean Set to true to delete the file.

isAutoClosingStream

  • Returns whether to automatically close the InputStream object after the download is complete.

  • Signatures

    public boolean isAutoClosingStream()
  • Return

    true, false

setAutoClosingStream

  • Sets whether to automatically close the InputStream object when downloading.

    Only affects when the InputStream is fed data to be downloaded.

  • Signatures

    public void setAutoClosingStream(boolean autoClosingStream)
  • Parameters

    Name Type Description
    autoClosingStream boolean If the parameter value is set to true, the InputStream object is closed after writing the data to the response buffer.

getExpiringTime

  • Returns the time value required to set the cache expiration response header.

  • Signatures

    public long getExpiringTime()
  • Return

    Time value in seconds to set the cache expired response header

setExpiringTime

  • Sets the time value required to set the cache expired response header.

  • Signatures

    public void setExpiringTime(long time)
  • Parameters

    Name Type Description
    time long Setting value in seconds

getUseTomcatSendFile (supported from version 1.2.0)

  • Returns whether Tomcat sendFile is used for file downloads.

  • Signatures

    public boolean getUseTomcatSendFile()
  • Return

    true or false. The default value is false.

setUseTomcatSendFile (supported from version 1.2.0)

  • Sets whether Tomcat sendFile is used for file downloads.

    (Caution) This setting cannot be used when HTTP compression is enabled or when response data is controlled through a servlet filter. If an error occurs, set this value to false to use the default download method.

  • Signatures

    public void setUseTomcatSendFile(boolean useTomatSendFile)
  • Parameters

    Name Type Description
    useTomatSendFile boolean true to use sendFile; otherwise false

download

  • Download a physical file or data in memory.

    It is recommended to use the download method with the FileDownloadOption parameter.

  • Signatures

    public void download(HttpServletRequest request, HttpServletResponse response, InputStream targetSteam, String filename, String mime, boolean inline)
    public void download(HttpServletRequest request, HttpServletResponse response, InputStream targetSteam, String filename, String mime)
    public void download(HttpServletRequest request, HttpServletResponse response, InputStream targetSteam, String filename)
    public void download(HttpServletRequest request, HttpServletResponse response, File target, String filename, String mime, boolean inline, boolean useClientCache)
    public void download(HttpServletRequest request, HttpServletResponse response, File target, String filename, String mime, boolean inline)
    public void download(HttpServletRequest request, HttpServletResponse response, File target, String filename, String mime)
    public void download(HttpServletRequest request, HttpServletResponse response, File target, String filename)
    public void download(HttpServletRequest request, HttpServletResponse response, File target)
    public void download(HttpServletRequest request, HttpServletResponse response, InputStream target, FileDownloadOption option)
    public void download(HttpServletRequest request, HttpServletResponse response, File target, FileDownloadOption option)
  • Parameters

    Name Type Description
    request javax.servlet.http.HttpServletRequest HttpServletRequest object
    response javax.servlet.http.HttpServletResponse HttpServletResponse object
    target java.io.File The File object to be downloaded
    targetSteam java.io.InputStream The InputStream object to be downloaded
    filename java.lang.String The file name to download
    mime java.lang.String

    Sets the MIME type of the file to be downloaded.

    If you don't know the MIME-TYPE of the target, you should set it to something like 'application/octet-stream'.

    inline boolean

    If you set the parameter value to true, the file will not be downloaded and may be opened directly in the browser.

    useClientCache boolean Set whether to use the client cache policy.
    option dextuploadjk.engine.FileDownloadOption An object with the option information needed to download the file.
  • Exception: IOException

  • Uses

    # Downloading from a file
    FileDownload fileDownload = new FileDownload();
    FileDownloadOption option = new FileDownloadOption();
    option.setFilename("manual.docx");
    option.setMime("appliation/octet-stream");
    fileDownload.download(request, response, new File("/data/F20220720-0001.docx"), option);
    # Download from data
    InputStream is = null;
    try {
        is = new ByteArrayInputStream(...);
      
        FileDownload fileDownload = new FileDownload();
        FileDownloadOption option = new FileDownloadOption();
        option.setFilename("manual.docx");
        option.setMime("appliation/octet-stream");
        fileDownload.download(request, response, is, option);
    } catch (Exception e) {
        ...
    } finally {
        if (is != null) is.close();
    }

downloadZip

  • Compresses the given file or directory into a zip file and downloads it immediately.

    The compressed file is removed internally as soon as the download is complete, and compressed downloads do not support partial content downloads.

  • Signatures

    public void downloadZip(HttpServletRequest request, HttpServletResponse response, String filename, List<File> targets, File tempZipDir, String encoding, boolean includeHiddenFile)
    public void downloadZip(HttpServletRequest request, HttpServletResponse response, String filename, List<File> targets, List<String> names, File tempZipDir, String encoding, boolean includeHiddenFile)
    public void downloadZip(HttpServletRequest request, HttpServletResponse response, String filename, File targetDir, boolean includeTargetDirName, File tempZipDir, String encoding, boolean includeHiddenFile)
  • Parameters

    Name Type Description
    request javax.servlet.http.HttpServletRequest HttpServletRequest object
    response javax.servlet.http.HttpServletResponse HttpServletResponse object
    filename java.lang.String

    Sets the filename to download.

    If null, the compressed filename is used.

    targets java.util.List<File> An object pointing to a list of files to compress.
    targetDir File An object pointing to the directory to compress.
    includeTargetDirName boolean

    Sets whether to include the root directory when compressing.

    If true, compresses the target directory, and if false, compresses child files or child directories without the target.

    tempZipDir File

    An object pointing to a temporary directory where the zip file will be created.

    If null, the operating system determines the location of the temporary directory.

    encoding java.lang.String

    Sets the charset name to use when encoding filenames containing multiple languages. Typically set to "UTF-8"; if null, use the charset set in the response object.

    includeHiddenFile boolean

    Sets whether to compress hidden files when compressing.

    names java.util.List<java.lang.String>

    A list of file names to compress, which should be the same size as the targets parameter.

  • Uses

    List<File> files = new ArrayList<File>();
    files.add(new File("/src/test/resources/compress/....txt"));
    files.add(new File("/src/test/resources/compress/....pdf"));
    files.add(new File("/src/test/resources/compress/subA/subB/....txt"));
    files.add(new File("/src/test/resources/compress/....jpg"));
    files.add(new File("/src/test/resources/compress/....docx"));
    		
    FileDownload dextnj = new FileDownload();
    dextnj.downloadZip(request, response, null, files, null, null, false);