Explanation
DEXTUploadX5 supports compressed downloads starting with version 1.3.0.0.
To receive multiple files simultaneously, a compressed download asks the server to pack the target files into a single compressed file, and then downloads the compressed file. Unlike the single/multiple file download examples in the other examples, compressed downloads are not affected by the attribute (downUrl) that specifies the download path to the target file because the compression process is handled by the server.
To perform a compressed download, you must specify the address that will handle the compression, and return the path value to download the compressed file using the setCompressURL method. Unlike single/multiple download, the downloadCompressed method is used when you start a compressed download.
var dx = dx5.get("component-id");
// The downUrl property is unnecessary.
// You must specify the value of the vindex property to distinguish targets on the server.
// vindex is a unique string value, which is the only way to distinguish between virtual files.
dx.addVirtualFile({ vindex: "IDX0003", name: "bridge_509147.jpg", size: 509147 });
dx.addVirtualFile({ vindex: "IDX0004", name: "beach_239826.jpg", size: 239826 });
dx.addVirtualFile({ vindex: "IDX0005", name: "cosmos (empty) 195779.jpg", size: 195779 });
// Set the address to process the compression and return the compressed file download path.
dx.setCompressURL("http://domain/path../service/compress.do");
// Perform the download according to the flag value.
// AUTO: Download compressed virtual files.
// SELECTED: Download compressed selected virtual files.
// CHECKED: Download compressed checked virtual files.
dx.downloadCompressed("SELECTED");
File compression and download are handled by the makeCompressedFile method of the FileServiceController and are mapped to compress.do. DEXTUploadX5 sends the form data with the name 'DEXTUploadX5_VIndexes' to the server in POST method when the downloadCompressed method is called, and the vindex property value of the virtual file to be downloaded is a list with a comma (,) as a separator.
If it is compressed, it must send the path to download the target compressed file to the client in the response.
@RequestMapping(value = "/service/compress.do", method = RequestMethod.POST)
public void makeCompressedFile(@RequestParam(value = "DEXTUploadX5_VIndexes") String vindices, HttpServletRequest request, HttpServletResponse response) throws IOException {
String fileRoot = servletContext.getRealPath("/files");
List<File> files = new ArrayList<File>();
String[] tokens = vindices.split(",");
for (int i = 0; i < tokens.length; i++) {
if (tokens[i].equals("IDX0003"))
files.add(new File(fileRoot, "attach/bridge_509147.jpg"));
if (tokens[i].equals("IDX0004"))
files.add(new File(fileRoot, "attach/beach_239826.jpg"));
if (tokens[i].equals("IDX0005"))
files.add(new File(fileRoot, "attach/cosmos (empty) 195779.jpg"));
}
// Create a compressed file in a temporary location.
CompressUtil cu = new CompressUtil();
File zipped = cu.zip(files, new File(fileRoot, "/temp/"), "UTF-8", false, false);
...
String compresskey = FileRepository.addFileEntity(target);
response.setContentType("text/plain");
// In the example, you can download the compressed file by requesting the same servlet as GET.
// The compresskey query string specifies what to download.
response.getWriter().write(request.getRequestURL().append("?compresskey=".concat(compresskey)).toString());
}
File downloads are handled by the downloadCompressedFile method of FileServiceController and are mapped to compress.do.
@RequestMapping(value = "/service/compress.do", method = RequestMethod.GET)
public ModelAndView downloadCompressedFile(@RequestParam(value = "compresskey") String key, HttpServletResponse response) throws IOException {
FileEntity target = FileRepository.getFileEntity(key);
if (target != null) {
DEXTUploadNJFileDownloadView dextnj = new DEXTUploadNJFileDownloadView();
dextnj.setFile(target.getFile());
dextnj.setCharsetName("UTF-8");
// Do not use partial content download function for compressed download. (Default)
dextnj.setAllowingWeakRange(false);
dextnj.setUseClientCache(false);
// Because the compressed file is a temporary file, delete it after the response data is written.
dextnj.setRemoveAfterDownloading(true);
return new ModelAndView(dextnj);
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND, "File not found for the key.");
return null;
}
}
Compressing files is a task that consumes a lot of server resources (CPU, I/O), so if you have a large number of files that are large files or compressed files, depending on the situation, it may take a long time to wait for the file compression process, which may lead to a problem of a session being disconnected or a serious problem that the service itself is stopped. Therefore, it is recommended to use the function after establishing policy restrictions for compressed download.
The DEXTUploadNJ product provides a class called CompressUtil for compression, which uses the Apache Commons Compress library. Refer to the DEXTUploadNJ product manual for details.
Example
|
Compress download |
|
Compression proceeds on the server. The progress window is displayed while the compression is in progress, but when the compressed file is downloaded, a single file download is performed. Therefore, the progress window for the downloading process is not provided. |