5) Use Memory Loading. |
A memory load is another form of 'file deployment'. To upload files to memory use a target(), targetAll() or targetEverything() method that doesn’t specify a specific location. Loading to memory means that all binary associated with the file is held in memory and not on disk.
IMPORTANT:- When memory loading a file, the memory used is equivalent to the maximum size setting for that upload. The default maximum size is 1GB but when using memory loading a maximum size always has to be set by the programmer. |
IMPORTANT:- When memory loading, only one file deployment for the memory load will exist even if multiple targets to memory have been set for that file upload. |
The advantages of memory loading are:
1. Performance:
Memory loading is quicker than loading to disk and therefore performance is improved.
XloadManager xman = new XloadManager(request); xman.target("file1", 2048); xman.upload();
|
where:
request - HttpServletRequest object.
file1 - File parameters inside html (or other) form.
The above code targets a file upload to memory only and is highly efficient. A maximum size of 2048 kilobytes (2MB) is given so that only a relatively small amount of memory is utilized. Also, if the final destination of the file is not to disk and is for example, a database BLOB field, then memory loading is the optimum solution.
2. Access to file names:
When memory loading, file names can be accessed and used or altered depending upon requirements. For example the file name could be checked for, within some destination directory and written accordingly.
1 XloadManager xman = new XloadManager(request); 2 xman.target("file1", 2048); 3 xman.upload(); 4 XloadFileUpload upload1 = xman.getFileUpload("file1"); 5 XloadFile f1 = upload1.getMemoryFile(); 6 XloadDirectory dir1 = xman.getDirectory("uploaded"); 7 if(!dir1.fileExists("target")){ 8 f1.resetTargetName("target"); 9 f1.moveTo("uploaded"); 10 }else{ 11 //inform user file name taken. 12 }
|
where:
request - HttpServletRequest object.
uploaded - Directory to upload files to (relative to the web application directory).
file1 - File parameter inside html (or other) form.
target - Arbitrary destination file name for uploaded file.
The above code would upload a file to memory and check to see if a certain file name existed within a specified directory and if it did not would move the file to that location with the new file name and clear the memory. This code does not take into account whether or not the upload fails in any way which Xload can provide detailed information on. This code can be used as is however and if a failure occurs a NullPointerException will be thrown (as object reference f1 will be null) and an error displayed. A variation of the above code can be seen below using a convenience method.
//Lines 4 and 5 replaced with:
XloadFile f1 = xman.getFile("file1");
|
3. Access to ordinary parameters:
Some ordinary form parameters (i.e. text fields) cannot be read before files are uploaded therefore if these ordinary parameters are needed for some decision making process then memory loading is the solution. For example, a 'project name' could be sent with a file upload, which needs to be read before the destination of the file is decided upon. The file is held in memory until the 'project name' is read and the file is written to disk accordingly. This is highly efficient, secure and provides an excellent solution.
XloadManager xman = new XloadManager(request); xman.target("file1", 2048); xman.upload(); XloadFile f1 = xman.getFile("file1"); String projectName = xman.getParameter("project"); XloadDirectory dir1 = xman.getDirectory("projects"); dir1 = dir1.createDirectory(projectName); f1.moveTo(dir1);
|
where:
request - HttpServletRequest object.
file1 - File parameter inside html (or other) form.
project - Ordinary form parameter
4. Security:
Loading to memory is the most secure way to load files if the final destination has to be decided on or is not a disk based location. For example, if the final destination is a Relational Database then memory loading is a highly efficient and secure solution allowing the file to move from memory directly into the BLOB field.
XloadManager xman = new XloadManager(request); xman.target("file1", 2048); xman.upload(); XloadFile f1 = xman.getFile("file1"); byte[] data = f1.getBytes(); //can now use byte array to write directly to a relational database;
|
where:
request - HttpServletRequest object.
file1 - File parameter inside html (or other) form.
The html on the client side for the above examples would look like:
<form action="/my_servlet" enctype="multipart/form-data" method="post"> file1: <input type="file" name="file1"> </form>
|
It must be noted that standard, uploading directly to disk could be used in some of the above examples but a number of disadvantages exist, mainly with performance and security. It may be needed in some circumstances though, because, if memory loading a file upload, the amount of memory equivalent to the maximum size for that upload is used. Therefore, if the file expected is potentially large then memory loading is not a viable option to use.
© Gubutech(Xload) 2006 (v1.2)