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

dextuploadjk.support.common
Class FileUpload

Minimum version
1.0.0
Minimum environment
Java 17
Description

FileUpload is a class that handles file upload operations from multipart/form-data requests.

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

Environment env = new Environment();
env.setCharEncoding("UTF-8");
env.setTempRepository(System.getProperty("java.io.tmpdir"));
env.setDefaultRepository("/file/attach");
env.setAutoMakingDirectory(true);
env.setMaxFileSize(1024 * 1024 * 20);
env.setMaxTotalSize(1024 * 1024 * 50);
env.setWhiteExtensions("jpg,gif,png,doc,xls,ppt,docx,xlsx,pptx,pdf,txt,zip,hwp");
env.setLicenseFilePath("/home/user/license/dextuploadjk.config");
FileUpload fileUpload = null;
try {
  fileUpload = new FileUpload(request, env);
  ...
  fileUpload.prepare();
  ...
} catch (...) {
  ...
} finally {
  if (fileUpload != null) fileUpload.close();
}

When you create a FileUpload object, you must call the prepare method to process the upload request. When the call to the prepare method is complete, the task of creating temporary files from the data sent from the client is complete.

Constructor
  • Create an object of class FileUpload.
  • Signatures

    public FileUpload(HttpServletRequest request)
    public FileUpload(HttpServletRequest request, Environment environment)
  • Parameters

    Name Type Description
    request javax.servlet.http.HttpServletRequest HttpServletRequest object
    environment dextuploadjk.engine.Environment Environment object
Methods

getEnvironment

  • Returns an Environment object with the upload configuration information.
  • Signatures

    public Environment getEnvironment()
  • Return

    Environment object

setEnvironment

  • Sets an Environment object with upload configuration information.
  • Signatures

    public void setEnvironment(Environment environment)
  • Parameters

    Name Type Description
    environment dextuploadjk.engine.Environment Environment object
  • Uses

    Environment env = new Environment();
    env.setCharEncoding("UTF-8");
    ...
    env.setLicenseFilePath("License file location");
    FileUpload fileUpload = new FileUpload(request); 
    fileUpload.setEnvironment(env);

isRemovingWhenClosing

  • Returns whether temporary files should be removed when the close method of the FileUpload object is called.

  • Signatures

    public boolean isRemovingWhenClosing()
  • Return

    The default value is true.

setRemovingWhenClosing

  • Sets whether temporary files should be removed when the close method of the FileUpload object is called.

  • Signatures

    public void setRemovingWhenClosing(boolean removingWhenClosing)
  • Parameters

    Name Type Description
    removingWhenClosing boolean true to remove temporary files, or false
  • Uses

    FileUpload fileUpload = null;
    try {
      fileUpload = new FileUpload(request, env);
      fileUpload.setRemovingWhenClosing(false);
      ...
      dextnj.prepare();
      ...
    } catch (...) {
      ...
    } finally {
      // Do not remove temporary files at close time.
      if (fileUpload != null) fileUpload.close();
    }

close

  • Remove all resources.

  • Signatures

    public void close()

flush

  • Consume all request data.

    Calling the flush method after the prepare method has already been called will raise an IllegalStateException exception.

  • Signatures

    public void flush()
  • Uses

    FileUpload fileUpload = new FileUpload(request, env); 
    ...
    fileUpload.flush();

prepare

  • Separates the string data items from the multipart request data and saves the uploaded file to a temporary directory location.

    This method must be called to perform the file upload service.

  • Signatures

    public void prepare()
  • Uses

    FileUpload fileUpload = new FileUpload(request, env); 
    ...
    fileUpload.prepare();

getMultipartCollection

  • Returns a MultipartCollection object with string and file fields.
  • Signatures

    public MultipartCollection getMultipartCollection()
  • Return

    dextuploadjk.engine.MultipartCollection object

  • Uses

    FileUpload fileUpload = new FileUpload(request, env); 
    ...
    MultipartCollection fileUpload = dextnj.getMultipartCollection();

getFormItemCount

  • Returns the number of string fields.
  • Signatures

    public int getFormItemCount()
  • Return

    String field count

  • Uses

    FileUpload fileUpload = new FileUpload(request, env); 
    ...
    int formCount = fileUpload.getFormItemCount();

getFileItemCount

  • Returns the number of file fields.
  • Signatures

    public int getFileItemCount()
  • Return

    File field count

  • Uses

    FileUpload fileUpload = new FileUpload(request, env); 
    ...
    int fileCount = fileUpload.getFileItemCount();

getFormItemNames

  • Returns all string field names as a java.util.Iterator object.
  • Signatures

    public Iterator<String> getFormItemNames()
  • Return

    java.util.Iterator object

  • Uses

    FileUpload fileUpload = new FileUpload(request, env); 
    ...
    Iterator<String> formNames = fileUpload.getFormItemNames();

getFileItemNames

  • Returns all file field names as a java.util.Iterator object.
  • Signatures

    public Iterator<String> getFileItemNames()
  • Return

    java.util.Iterator object

  • Uses

    FileUpload fileUpload = new FileUpload(request, env); 
    ...
    Iterator<String> formNames = fileUpload.getFileItemNames();

getFormItem

  • Returns a FormItem object corresponding to the given order or names.

    If multiple fields with the same name exist, the first field is returned.

  • Signatures

    public FormItem getFormItem(int index)
    public FormItem getFormItem(String name)
  • Parameters

    Name Type Description
    index int Order values
    name java.lang.String Name
  • Return

    FormItem object

  • Uses

    FileUpload fileUpload = new FileUpload(request, env); 
    ...
    FormItem formitem = fileUpload.getFormItem(index);

getFormItemValue

  • Returns the value of the string field corresponding to the given order or name.

  • Signatures

    public String getFormItemValue(int index)
    public String getFormItemValue(String name)
  • Parameters

    Name Type Description
    index int Form item order
    name java.lang.String Form name
  • Return

    String field value

  • Uses

    FileUpload fileUpload = new FileUpload(request, env); 
    ...
    String strValue = fileUpload.getFormItemValue(index);
    // or
    String strValue = fileUpload.getFormItemValue("Form field name");

getFormItems

  • Returns a list of fields (java.util.List<FormItem>) corresponding to all or the given name.

  • Signatures

    public List<FormItem> getFormItems()
    public List<FormItem> getFormItems(String name)
  • Parameters

    Name Type Description
    name java.lang.String Name
  • Return

    java.util.List<FormItem> object

  • Uses

    FileUpload fileUpload = new FileUpload(request, env); 
    ...
    List<FormItem> formitems = fileUpload.getFormItems(); 
    // or
    List<FormItem> formitems = fileUpload.getFormItems("Form field name");

getFileItem

  • Returns the FileItem object corresponding to the given order or name.

    If multiple fields with the same name exist, the first field is returned.

  • Signatures

    public FileItem getFileItem(int index)
    public FileItem getFileItem(String name)
    public FileItem getFileItem()
  • Parameters

    Name Type Description
    index int Order values
    name java.lang.String Name
  • Return

    FileItem object

  • Uses

    FileUpload fileUpload = new FileUpload(request, env); 
    ...
    FileItem fileitem = fileUpload.getFileItem(index);

getFileItems

  • Returns a list of files (java.util.List<FileItem>) corresponding to all or the given name.

  • Signatures

    public List<FileItem> getFileItems()
    public List<FileItem> getFileItems(String name)
  • Parameters

    Name Type Description
    name java.lang.String Name
  • Return

    java.util.List<FileItem>

  • Uses

    FileUpload fileUpload = new FileUpload(request, env); 
    ...
    List<FileItem> fileitem = fileUpload.getFileItems();
    // or
    List<FileItem> fileitem = fileUpload.getFileItem("File field name");

saveAll

  • Save (move/copy) all temporary files.

    We recommend using a method with the BulkSaveOption parameter.

  • Signatures

    public void saveAll(boolean overwrite)
    public void saveAll(boolean overwrite, boolean copy)
    public void saveAll(String targetDirectoryPath)
    public void saveAll(String targetDirectoryPath, boolean overwrite)
    public void saveAll(String targetDirectoryPath, boolean overwrite, boolean copy)
    public void saveAll(BulkSaveOption option)
  • Parameters

    Name Type Description
    targetDirectoryPath java.lang.String Where to save the files
    overwrite boolean

    If a file with the same name exists at the destination, it will be overwritten if the parameter value is true, or saved with a non-duplicate filename if it is false.

    When saving a file, DEXTUploadJK automatically appends a suffix value to any files with the same filename. However, on systems that serve many requests simultaneously, such as the web, it is likely that identical filenames will be generated despite the suffixing. Therefore, even if the overwrite argument is set to the default value or false, you will often get an error that causes the file write to fail. To avoid this situation as much as possible, you should not save the original filename, but give it a name that is as non-duplicative as possible.

    copy boolean

    If the parameter value is true, temporary files are not removed after saving the file. If false, remove the temporary file immediately after saving.

    option BulkSaveOption

    Object to set options for saving a temporary file.

  • Uses

    FileUpload fileUpload = new FileUpload(request, env); 
    ...
    // The most basic full save method
    fileUpload.saveAll();
    // Method 2
    fileUpload.saveAll("/home/user/others/");
    // Method 3
    fileUpload.saveAll("/home/user/others/", true);
    // Method 4
    fileUpload.saveAll("/home/user/others/", true, true);
    // Method 5
    BulkSaveOption option = new BulkSaveOption();
    option.setTargetDirectoryPath("/home/user/others/");
    option.setOverwrite(true);
    option.setCopy(true);
    fileUpload.saveAll(option);