www.dextsolution.com
DEXTUPLOAD
NJ
menu toggleProduct description > Uploading large files

Uploading large files

Uploading files with a size of 2 GB or more in DEXTUploadNJ is referred to as a "large file upload". DEXTUploadNJ provides the ability to handle large file uploads when used in conjunction with our component products such as DEXTUpload X5 (based on HTML5). (Please note that large file uploads are only possible in conjunction with our components and are not supported by DEXTUploadNJ alone.)

Large file uploads are handled differently than normal file uploads. While a normal file upload can send all 10 files in a single request, a large file upload can send at least 20 to thousands of requests to the server, depending on the situation. Since DEXTUploadNJ's large file upload feature natively supports concatenation, the product communicates with the server in advance, and since it sends the file in chunks, the number of requests increases with the size of the chunks. For example, if you upload 10 files of 2.5 GB in size and transfer them in chunks of about 10 MB each, you can expect to make about 2500 requests. However, since DEXTUploadNJ takes care of the task of combining the fragmented files into one, the developer only has to deal with storing (transferring) the files to the destination 10 times in total.

The ability to upload large files is entirely handled by the ExtensionFileUploadFilter class, which implements the javax.servlet.Filter interface. Because it is a filter class, it is automatically loaded by the Java servlet container (like Tomcat) and handles large file uploads without any code-level creation or invocation.

Here's how to set up the ExtensionFileUploadFilter filter in DD (web.xml)

# web.xml
<filter>
  <filter-name>extensionUploadFilter</filter-name>
  <filter-class>devpia.dextuploadnj.support.common.ExtensionFileUploadFilter</filter-class>
  <!-- omit setting parameters -->
</filter>
<filter-mapping>
  <filter-name>extensionUploadFilter</filter-name>
  <!-- 
  Map the servlet or URL where you want large uploads to be processed.
  <servlet-name>Servlet name</servlet-name>
  <url-pattern>Mapping URL</url-pattern>
  -->
</filter-mapping>
For Servlet or JSP environments (versions prior to 2.5.0)

The temporary file information processed by the ExtensionFileUploadFilter filter can be obtained by using the getAttribute method of the HttpServletRequest object in the servlet or JSP that the developer needs to write.

// Get a MultipartCollection object with the filter's processing result using DEXTUPLOADNJ_EXTENSION_FILE_UPLOAD_RESULT as the key.
MultipartCollection multiparts = (MultipartCollection)request.getAttribute(Definition.DEXTUPLOADNJ_EXTENSION_FILE_UPLOAD_RESULT);
try {
    FileItem item = multiparts.getFileItem(0);
    
    if (item.isEmpty()) {
        // If the file is empty, you should generally raise an exception.
    }
    
    // Save (copy or move) the temporary file to the actual storage location.
    // If no directory path is given as an argument, the path set by the value of the defaultRepository parameter of ExtensionFileUploadFilter is targeted.
    String path = item.save();
        
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/plain");
    // You can write any value to the response object, but it's common to write a key or a file name or path that represents a unique value for the file.
    response.getWriter().write(path);
} finally {
    if (multiparts != null) {
        // Remove the resource. Any temporary files that have not been deleted will be deleted.
        multiparts.deleteTempFiles();
        multiparts.clear();
    }
}
For Servlet or JSP environments (2.5.0 and later)

Starting with version 2.5.0, you can reduce the complicated preliminary steps and use the FileUpload class to get the file item.

FileUpload dextnj = null;
try {
    dextnj = new FileUpload(request);
    dextnj.prepare();

    FileItem item = dextnj.getFileItem();
    if (item.isEmpty()) {
        // If the file is empty, you should normally raise an exception.
    }
  
    String path = item.save();
  
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/plain");
    response.getWriter().write(path);
} finally {
    if (dextnj != null) dextnj.close();
}
For Spring Web Framework environments (versions prior to 2.6.0)

In a Spring environment, not only do you need to set the ExtensionFileUploadFilter in the DD(web.xml), but you also need to set the MultipartResolver bean in the XML file responsible for setting the DispatcherServlet object.

# web.xml
<servlet>
  <servlet-name>defaultDispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
  </init-param>
</servlet>

Open the dispatcher-servlet.xml file to declare the DEXTUploadNJMultipartResolver bean.

# dispatcher-servlet.xml
<bean id="multipartResolver" class="devpia.dextuploadnj.support.spring.DEXTUploadNJMultipartResolver"/>

If you have CommonMultipartResolver or StandardServletMultipartResolver (provided by Spring) set up, you should delete them. These beans perform the same task as the DEXTUploadNJMultipartResolver bean, so setting them at the same time may cause an error.

The temporary file information processed through the ExtensionFileUploadFilter filter can be received as a MultipartFile object in the method mapped to the Controller, where the final saving operation can be performed.

@RequestMapping(value = "/extension-upload.do", method = RequestMethod.POST)
public void upload(
    // If this is a DEXTUploadX5 product, it will be passed as "DEXTUploadX5_FileData".
    @RequestParam(value = "DEXTUploadX5_FileData") MultipartFile file, 
    HttpServletResponse response) throws IOException {
    
    // Cast to the FileItem interface.
    FileItem item = (FileItem)file;	
    
    if (item.isEmpty()) {
        // If the file is empty, an exception should normally be thrown.
        // This is because a client like DEXTUploadX5 will not send a request to the server with no file.
    }  
    
    String path = item.save();
    
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/plain");
    response.getWriter().write(path);
}
For Spring Web Framework environments (versions 2.6.0 and later)

DEXTUploadNJ version 2.6.0 provides a dedicated filter, DEXTUploadNJSpringExtensionUploadFilter, for use in Spring environments.

# web.xml
<filter>
  <filter-name>extensionUploadFilter</filter-name>
  <filter-class>devpia.dextuploadnj.support.spring.DEXTUploadNJSpringExtensionUploadFilter</filter-class>
  <!-- omit setting parameters -->
</filter>
<filter-mapping>
  <filter-name>extensionUploadFilter</filter-name>
  <!-- 
  Map the servlet or URL where you want large uploads to be processed.
  <servlet-name>Servlet name</servlet-name>
  <url-pattern>Mapping URL</url-pattern>
  -->
</filter-mapping>

If you use DEXTUploadNJSpringExtensionUploadFilter instead of ExtensionFileUploadFilter in your DD (web.xml), you can skip setting up the DEXTUploadNJMultipartResolver bean. Other than that, everything else is the same as using the partial ExtensionFileUploadFilter.