www.dextsolution.com
DEXTUPLOAD
NJ
menu toggleReference > devpia > dextuploadnj > support > common > FileDownload

devpia.dextuploadnj.support.common
Class FileDownload

Minimum version supported
1.0.0
Minimum support environment
JRE 1.6
Description

FileDownload is a class that provides the function to download a physical file and data existing in memory as a file.

# Downloading a physical file

FileDownload dextnj = new FileDownload();
// Setting options
FileDownloadOption option = new FileDownloadOption();
option.setFilename("sea.png");
// Downloading
dextnj.download(request, response, new File("target path"), "file name to download", mime, inline);
# Downloading memory data

FileDownload downloader = new FileDownload();
// Setting options
FileDownloadOption option = new FileDownloadOption();
option.setFilename("profile.txt");
// Downloading
dextnj.download(request, response, inputStream, "file name to download", mime, inline);
Constructor
  • Create an object of the FileDownload class.
  • Signatures

    public FileDownload()
Methods

isAllowingWeakRange

  • Returns whether or not to execute Partial Content download with only the Range request header.

    (Partial Content downloading is to return a part of data to response data.)

  • Signatures

    public boolean isAllowingWeakRange()
  • Returns

    true, false

setAllowingWeakRange

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

    (Partial Content downloading is to return a part of data to response data.)

  • Signatures

    public void setAllowingWeakRange(boolean allowingWeakRange)
  • Parameters

    Name Type Description
    allowingWeakRange boolean If true, processes with Range header only, if false, checks if-Range header.

isRemoveAfterDownloading (Supported since version 1.2.0)

  • After downloading the file, returns whether or not to delete the target file.

  • Signatures

    public boolean isRemoveAfterDownloading()
  • Returns

    true, false

setRemoveAfterDownloading (Supported since version 1.2.0)

  • After downloading the file, sets whether or not to delete the target file.

    The file deletion function operates only when downloading a physical file on disk, and file deletion can fail if another system preempted the target file.

  • Signatures

    public void setRemoveAfterDownloading(boolean removeAfterDownloading)
  • Parameters

    Name Type Description
    removeAfterDownloading boolean true, false

isAutoClosingStream (Supported since version 1.3.0)

  • After the download is completed, returns whether or not to close the InputStream object automatically.

  • Signatures

    public boolean isAutoClosingStream()
  • Returns

    true, false

setAutoClosingStream (Supported since version 1.3.0)

  • After the download is completed, sets whether or not to close the InputStream object automatically.

    It only affects when downloading from InputStream.

  • Signatures

    public void setAutoClosingStream(boolean autoClosingStream)
  • Parameters

    Name Type Description
    autoClosingStream boolean When setting the value of this parameter to true, the data is written to the response buffer, and then the InputStream object is closed.

getExpiringTime (Supported from 2.2.0 version)

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

  • Signatures

    public long getExpiringTime()
  • Returns

    the time value in seconds required to set the cache expiration response header

setExpiringTime (Supported from 2.2.0 version)

  • Sets the time value requrited to set the cache expiration response header.

  • Signatures

    public void setExpiringTime(long time)
  • Parameters

    Name Type Description
    time long the time value in seconds

getUseTomcatSendFile (Supported from 2.14.0 version)

  • Returns whether to use Tomcat sendFile for file downloads.

  • Signatures

    public boolean getUseTomcatSendFile()
  • Returns

    true or false, the default value is false.

setUseTomcatSendFile (Supported from 2.14.0 version)

  • Sets whether to use Tomcat sendFile for file downloads.

    (Caution) This setting cannot be used when HTTP compression is enabled or when the 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

  • Downloads a physical file or data on memory.

  • 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)
    
    # Since version 2.7.0
    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 the HttpServletRequest object
    response javax.servlet.http.HttpServletResponse the 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

    The MIME type of the file to be downloaded. If you do not know the target MIME type, you need to set the "application/octet-stream".

    inline boolean

    If the value of this parameters is set to true, the file will not be downloaded and may be opened directly from the browser.

    useClientCache boolean whether to use the client's cache policy
    option (Supported since version 2.7.0) devpia.dextuploadnj.FileDownloadOption The object that has options for downloading
  • Exception: IOException

  • How to use

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

downloadZip (Supported since version 1.3.0)

  • Compresses the specified files or directory to create a zip file, and downloads it immediately.

    When the download is completed, the compressed file is deleted directly internally, and the compressed download does not support the partial content downloading.

  • 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 the HttpServletRequest object
    response javax.servlet.http.HttpServletResponse the HttpServletResponse object
    filename java.lang.String

    the file name to download. If null, it uses the compressed file name as it is.

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

    Whether to include the root directory when compressed. If true, compression including the target directory is performed, and when it is false, the child file or the child directory excluding the target is compressed.

    tempZipDir File

    An object that points to the 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

    The character-set name to be used in the process of encoding the file name containing the multilingual language. In general, it is set to "UTF-8", and if it is null, the character set in the response object is used.

    includeHiddenFile boolean

    whether to compress hidden files

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

    (Supported since version 2.4.0)

    A list of names of files to be compressed, the size of the 'names' parameter is equal to the size of the 'targets' parameter.

  • How to use

    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);