11) Copy Files.

 

back to main index

 

To copy files to another location the procedure is simple. Use the following code to copy a disk based file deployment to another location, retaining its original name ('remote name') and copy 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    XloadFile copy1 = f1.copyTo("copied");

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

9    XloadFile f2 = upload2.getMemoryFile();

10   f2.resetTargetName("new");

11   XloadFile copy2 = f2.copyTo("copied");

 

 

 

where:

request - HttpServletRequest object.

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

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

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

   

 

Notice how the name of the new file is created via the use of the resetTargetName() method which allows a copy to have a different name. Initially the target name of a file 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 creating a copy.

 

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

 

 

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

 

//Replace lines 10 and 11 with:

 

 

XloadFile copy2 = f2.copyTo("copied");

copy2.renameTo("new");

 

 

 

The above code alteration runs the risk of a name clash inside the 'copied' directory as we are copying using the original file name ('remote name') initially. If a name clash occurs while copying, 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 copying, 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{

XloadFile copy1 = f1.copyTo("copied");

}catch(XloadFileException e){

//a name clash has occurred while copying.

}

 

 

 

where:

request - HttpServletRequest object.

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

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

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

     

 

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

 

 

try{

  XloadFile copy1 = f1.copyTo("copied");

  XloadFile copy2 = f2.copyTo("copied");

  XloadFile copy3 = f3.copyTo("copied");

  XloadFile copy4 = f4.copyTo("copied");

}catch(XloadFileException e){

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

}

 

 

 

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

 

 

XloadManager xman = new XloadManager();

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

XloadFile copy = file.copyTo("c:\copied");

 

 

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 any file that is created from it (i.e. a copy and subsequent copies) is also termed a file deployment. During subsequent requests though, these file deployments will become arbitrary files.

 

back to main index

top of page

 

 

 

 

 

 

© Gubutech(Xload) 2006  (v1.2)