12) Move Files.

 

back to main index

 

To move files to another location using Xload, the procedure is easy. Use the following code to move a disk based file deployment to another location, retaining its original name ('remote name'); and move a memory loaded file deployment to the same location changing its name to a user provided name.

 

 

1    XloadManager xman = new XloadManager(request);

2    xman.target("file1", "uploaded");

3    xman.target("file2", 1024);

4    xman.upload();

5    XloadFileUpload upload1 = xman.getFileUpload("file1");

6    XloadFile f1 = upload1.getFile(1);

7    f1.moveTo("moved");

8    XloadFileUpload upload2 = xman.getFileUpload("file2");

9    XloadFile f2 = upload2.getMemoryFile();

10   f2.resetTargetName("new");

11   XloadFile move2 = f2.moveTo("moved");

 

 

 

 

where:

request - HttpServletRequest object.

uploaded - Directory to upload files to (relative to the web application directory).

moved - Directory to copy files to (relative to the web application directory).

file* - File parameter inside html (or other) form.

 

Notice how the name is changed via the use of the resetTargetName() method which allows this file to have a different name when moved. Initially a target name is set to the files written name (or remote name in the case of a memory loaded file) and it is this target name that is used when moving the file.

 

IMPORTANT:- Normally, when the written name of a file is changed its target name changes to the same value. After resetTargetName() is called then the target name can be altered by only this method.

 

Also notice that when moving the disk based file deployment (f1), no new object is returned as the file has just been moved from one directory to another on disk. In the case of the memory loaded file deployment (f2) however, a new object is returned, as a new type of file deployment has been created (even though it represents the original memory loaded file deployment) and added to the list of file deployments while the original memory loaded file deployment is deleted.

 

The same result could have been achieved by using the following code.

 

//Replace lines 10 and 11 with:

 

 

XloadFile move2 = f2.moveTo("moved");

move2.renameTo("new");

 

 

 

 

The above code alteration runs the risk of a name clash inside the ‘moved’ directory as we are moving using the original file name initially. If a name clash occurs while moving, a ServletException is thrown, which doesn’t normally need to be handled as it will be declared to be thrown by default if using Xload inside a Servlet or JSP. But, if you wish to handle the situation whereby a naming clash occurs while moving then a subclass of ServletException can be caught and dealt with. The code below demonstrates.

 

 

XloadManager xman = new XloadManager(request);

xman.target("file1", "uploaded");

xman.upload();

XloadFileUpload upload1 = xman.getFileUpload("file1");

XloadFile f1 = upload1.getFile(1);

try{

f1.moveTo("moved");

}catch(XloadFileException e){

//a name clash has occurred while moving.

}

 

 

 

 

where:

request - HttpServletRequest object.

uploaded - Directory to upload files to (relative to the web application directory).

moved - Directory to move files to (relative to the web application directory).

file1- File parameter inside html (or other) form.

     

 

If you were to move a number of files and a name clash occurs, then the offending file can be ascertained by the use of the getFile() method of the XloadFileException class as shown below.

 

 

try{

f1.moveTo("moved");

f2.moveTo("moved");

f3.moveTo("moved");

XloadFile new = f4.moveTo("moved");  //memory load

}catch(XloadFileException e){

XloadFile f = e.getFile();  //gets offending file (f1,f2,f3,f4)

}

 

 

 

 

Another example of moving a file is by accessing an arbitrary file on the file system and then moving to another location as demonstrated below:

 

 

XloadManager xman = new XloadManager();

XloadFile file = xman.getAnyFile("c:\upload\file.txt");

file.moveTo("c:\moved");

 

 

 

 

Notice the use of the default XloadManager constructor and consequent needed use of absolute paths (windows™ specific in this case). The use of the method getAnyFile() returns an arbitrary file that may or may not be a file deployment.

 

IMPORTANT:- A file that has been written from an upload (disk or memory) is termed a file deployment and even if this file is moved it still remains a file deployment. Subsequent requests will though, see these file deployments as arbitrary files in the future.

 

back to main index

top of page

 

 

 

 

 

© Gubutech(Xload) 2006  (v1.2)