zehon.com: FTP Tutorial

FTP for Java


FTP Tutorial

Table of content


# About Zehon FTP

Zehon FTP for Java is a versatile file-transfer component for Java language. It enables you to transfer or download files directly from/to your application using FTP.  It can be used in a rich client application or in a web container.

 

back to top...


# Uploading and downloading files

File transfers are an essential part of the FTP protocol and can be achieved using the sendFile and getFile methods. They accept the path to the local file and the path to the remote file (both paths must include the filename) and return the status of the transfer.   Please prefer to the JavaDoc for more information.

Short way (uploading a file):

// upload C:\myfiles\test.txt to remote folder \myftpFolder 
//with ftp server ftp.myhost.com, username ftp and password pass
     
try{    
  int status = FTP.sendFile("C:\myfiles\test.txt", "/myftpFolder", "ftp.myhost.com", 
                                                             "ftp", "pass");
}catch(FileTransferException ex){
  e.printStackTrace();
}  


Longer way (uploading a file):

// upload C:\myfiles\test.txt to remote folder \myftpFolder 
//with ftp server ftp.myhost.com, username ftp and password pass
     
FTPClient ftpClient = new FTPClient("ftp.myhost.com","ftp", "pass");
try{    
  int status = FTP.sendFile("C:\myfiles\test.txt", "/myftpFolder");
}catch(FileTransferException ex){
  e.printStackTrace();
}  

Short way (uploading a file represented by java.io.InputStream):

// upload an java.io.InputStream representing C:\myfiles\test.txt to 
//remote folder \myftpFolder 
//with ftp server ftp.myhost.com, username ftp and password pass
     

InputStream is = null;
String filePath = "C:\\myfiles\\test.txt";
File localFile = new File(filePath);        
try {
	is = new BufferedInputStream(new FileInputStream(localFile));
} catch (FileNotFoundException e1) {
	// TODO Auto-generated catch block
	e1.printStackTrace();
}
String destFolder = "/myftpFolder";
String nameOfFile = "testStream.txt";
int status = -1;
try {
   
	status = FTP.sendFile(is, nameOfFile, destFolder, 
	                            "ftp.myhost.com", "ftp", "pass" );
} catch (FileTransferException e) {
	e.printStackTrace();
	//or
	//ex.getCause().printStackTrace();  (
	//Depending on what version of JDK you use)			
}finally{
	if(is != null){try {is.close();} catch (IOException e) {}}
}


Longer way (uploading a file represented by java.io.InputStream):

// upload an java.io.InputStream representing C:\myfiles\test.txt to 
//remote folder \myftpFolder 
//with ftp server ftp.myhost.com, username ftp and password pass
     
InputStream is = null;
String filePath = "C:\\myfiles\\test.txt";
File localFile = new File(filePath);        
try {
	is = new BufferedInputStream(new FileInputStream(localFile));
} catch (FileNotFoundException e1) {
	e1.printStackTrace();
}
String destFolder = "/myftpFolder";
String nameOfFile = "testStream.txt";
int status = -1;
try {
    FTPClient ftpClient = new FTPClient("ftp.myhost.com", "ftp", "pass" );
	status = ftpClient.sendFile(is, nameOfFile, destFolder);
} catch (FileTransferException e) {
	e.printStackTrace();
	//or
	//ex.getCause().printStackTrace();  
	//(Depending on what version of JDK you use)			
}finally{
	if(is != null){try {is.close();} catch (IOException e) {}}
}

 

Short way (downloading a file):

// downloading testStream.txt from remote folder \test
//to local folder C:\\myfiles\\writeToFolder
//with ftp server ftp.myhost.com, username ftp and password pass
     
String ftpFolder = "/test";
String nameOfFile = "testStream.txt";
String toLocalFolder = "C:\\myfiles\\writeToFolder";
int status = -1;
try {		    
	status = FTP.getFile(nameOfFile, ftpFolder, toLocalFolder,
	                                 "ftp.myhost.com", "ftp", "pass");
	
} catch (Exception e) {
	e.printStackTrace();
	//or
	//ex.getCause().printStackTrace();  
	//(Depending on what version of JDK you use)					
}

Longer way (downloading a file):

// downloading testStream.txt from remote folder \test
//to local folder C:\\myfiles\\writeToFolder
//with ftp server ftp.myhost.com, username ftp and password pass
     
String ftpFolder = "/test";
String nameOfFile = "testStream.txt";
String toLocalFolder = "C:\\myfiles\\writeToFolder";
int status = -1;
try {
    FTPClient ftpClient = new FTPClient("ftp.myhost.com", "ftp", "pass");
	status = ftpClient.getFile(nameOfFile, ftpFolder, toLocalFolder);
	
} catch (Exception e) {
	e.printStackTrace();
	//or
	//ex.getCause().printStackTrace();  
	//(Depending on what version of JDK you use)					
}

Short way (downloading a file represented by java.io.InputStream):

// downloading testStream.txt from remote folder \test
//the result is java.io.InputStream
//with ftp server ftp.myhost.com, username ftp and password pass
     

InputStream is = null;		
String destFolder = "/test";
String nameOfFile = "testStream.txt";
try {
	
	is = FTP.getFileAsStream(nameOfFile, destFolder, 
	                            "ftp.myhost.com", "ftp", "pass" );
} catch (Exception e) {
	e.printStackTrace();			
	//or
	//ex.getCause().printStackTrace();  
	//(Depending on what version of JDK you use)
}finally{
	if(is != null){try {is.close();} catch (IOException e) {}}
}

Longer way (downloading a file represented by java.io.InputStream):

// downloading testStream.txt from remote folder \test
//the result is java.io.InputStream
//with ftp server ftp.myhost.com, username ftp and password pass
     
InputStream is = null;		
String destFolder = "/test";
String nameOfFile = "testStream.txt";
try {
	FTPClient ftpClient = new FTPClient("ftp.myhost.com", "ftp", "pass");
	is = ftpClient.getFileAsStream(nameOfFile, destFolder);			
} catch (Exception e) {
	e.printStackTrace();			
	//or
	//ex.getCause().printStackTrace();  
	//(Depending on what version of JDK you use)
}finally{
	if(is != null){try {is.close();} catch (IOException e) {}}
}

 

back to top...



# Working with directories

Working with directories (folders) on the FTP server is simple. The remote filesystem is organized in the same way as in Un*x. If you are used to Windows, watch out for the two differencies - a slash (/) is used instead of a backslash, and there is only a single root at "/", no drive letters. A typical path to a file might look like "/pub/incoming/test.zip", for example.

Coming soon! Please prefer to the JavaDoc for more information.

back to top...


# List of files and directories

The following code snippet dispalys the list of files in the remote directory to a console:

Coming soon! Please prefer to the JavaDoc for more information.

back to top...


# Transfering multiple files and directories

Upload or download of multiple files is a very common task. There are getFolder and sendFolder methods that can be used to transfer multiple files easily - just provide the source path (which can be a directory or contain wildcards), destination path and transfer options.

When transferring lots of files, things can occasionally go wrong due to unforeseen problems - to be informed about such errors, use BatchTransferProgress event that also makes it possible to select the desired next action. To stay informed about what is currently going on, use the BatchTransferProgress event.

For more information about these events, check out the BatchTransfer sample application!

Coming soon! Please prefer to the JavaDoc for more information.

 

back to top...