8) Access File Deployments after an Upload. |
After an upload has taken place Xload can easily access file deployments whether on disk or in memory using a number of different techniques. These different techniques are demonstrated below:
IMPORTANT:- When file deployments are created for each file uploaded, they create a FIFO (First In First Out) list and are added to this list in the order they were targeted. These file deployments are then retrieved using this FIFO list. The exception is the memory file deployment which is always placed at the head of the FIFO list. |
IMPORTANT:- When using the targetAll() and targetEverything() family of methods, targeting is actually set when the upload() method is called. Targeting provided by any targetEverything() calls are set first, followed by targeting provided by any targetAll() calls. This is done just before uploading takes place (called Just In Time Targeting - JITT). Consequently, any targets set by these methods will appear last in the FIFO list when retrieved by the techniques demonstrated below. |
Firstly, we can access file deployments by using the XloadFileUpload interface and use the getFile(int) method to access file deployments. Using this method we see that any memory loaded file for a file upload is always first in the list of stored file deployments.
1 XloadManager xman = new XloadManager(request); 2 xman.targetAll("uploadedbackup"); 3 xman.target("file1", "uploadedfirst"); 4 xman.target("file1", "uploadedsecond"); 5 xman.target("file1", 4096); 6 xman.target("file2", "uploaded", 1024); 7 xman.target("file3", "uploaded", 2048); 8 xman.upload();
9 XloadFileUpload upload1 = xman.getFileUpload("file1"); 10 XloadFile memory = upload1.getFile(1); 11 XloadFile specificTarget1 = upload1.getFile(2); 12 XloadFile specificTarget2 = upload1.getFile(3); 13 XloadFile targetAll = upload1.getFile(4);
|
where:
request - HttpServletRequest object.
uploaded* - directories to upload files to (relative to the web application directory).
file* - File parameter inside html (or other) form.
upload1 - XloadFileUpload object representing the file upload for file parameter file1.
memory - XloadFile object representing memory based file deployment.
specificTarget1 - XloadFile object representing file deployment inside uploadedfirst directory.
specificTarget2 - XloadFile object representing file deployment inside uploadedsecond directory.
targetAll - XloadFile object representing file deployment inside uploadedbackup directory.
Secondly, you can use the XloadFileUpload interface and access file deployments using more direct methods, accessing directly the memory loaded file and accessing disk based files by specifying a directory to search in for them (these methods will always return the file deployment that is higher up the FIFO list and is in the specified directory).
//Replace Lines 9 - 13 with the Following:
XloadFileUpload upload1 = xman.getFileUpload("file1"); XloadFile memory = upload1.getMemoryFile(); XloadFile specificTarget1 = upload1.getFile("uploadedfirst"); XloadFile targetAll = upload1.getFile("uploadedbackup");
|
You can use convenience methods provided by the XloadManager class to perform the same functionality as above by stating the file parameter name from the initial request.
//Replace Lines 9 - 13 with the Following:
XloadFile memory = xman.getMemoryFile("file1"); XloadFile specificTarget1 = xman.getFile("file1", "uploadedfirst"); XloadFile targetAll = xman.getFile("file1", "uploadedbackup");
|
If you have a number of file deployments and you want to systematically iterate through them, Xload provides facilities to do this. Firstly, you can just iterate through the whole FIFO list by using the following.
//Replace Lines 9 - 13 with the Following:
XloadFileUpload upload1 = xman.getFileUpload("file1"); while(upload1.hasMore()){ XloadFile file = upload1.getNext(); }
|
Secondly, you can iterate through the FIFO list by specifying a directory which narrows the number of file deployments returned (the reset() method resets the list back to the beginning).
upload1.reset(); while(upload1.hasMore("uploadedfirst")){ XloadFile file = upload1.getNext(); }
|
Instead of using a String for the specified directory you can use an XloadDirectory object as shown below.
upload1.reset(); XloadDirectory dir = xman.getDirectory("uploadedbackup"); while(upload1.hasMore(dir)){ XloadFile file = upload1.getNext(); }
|
You can access file deployments by using the getAnyFile() method provided by the XloadManager class. This method will return a file deployment or an ordinary file on the file system depending on the selected file (Remember, File deployments only exist as file deployments for the life of the XloadManager object they were created from after which they revert to being arbitrary files). The code below demonstrates.
If we have a file to upload called file.txt which is uploaded using a file parameter name file1 then:
XloadManager xman = new XloadManager(request); xman.target("file1", "uploaded"); xman.upload(); XloadFile file = xman.getAnyFile("uploaded", "file.txt");
|
The above code snippet retrieves the file deployment just uploaded (if the file uploaded is not renamed). Examine the following code snippet uploading the same file as above:
XloadManager xman = new XloadManager(request); xman.target("file1", "uploaded"); xman.upload(); XloadFile file = xman.getAnyFile("somefolder", "somefile.txt");
|
The above code snippet retrieves an ordinary file on the file system and not a file deployment.
Xload provides methods to directly access different results of uploading, which include the following:
getFailedFileUploads() which retrieves all file uploads that were attempted but failed due to one of the reasons as an upload failure.
getSuccessfulFileUploads() which retrieves all file uploads that were successful.
getRedundantTargetUploads() which retrieves all file uploads that were specifically targeted using the target() family of methods.
getAttemptedTargetUploads() which will retrieve all file uploads that were attempted whether successful or not.
How these are accessed is shown below:
XloadManager xman = new XloadManager(request); ... ... xman.upload();
List uploadFailures = xman.getFailedFileUploads(); List successfulUploads = xman.getSuccessfulFileUploads(); List redundantUploads = xman.getRedundantTargetUploads(); List redundantUploads = xman.getAttemptedTargetUploads();
|
where:
request - HttpServletRequest object.
© Gubutech(Xload) 2006 (v1.2)