The FTP (File Transfer Protocol), FTPS (FTP over SSL), SFTP (FTP over SSH) and SCP (Secure Copy over SSH) . 

http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPSClient.html


The examples of FTP/FTPs, the jar supporting them is org.apache.commons.net.ftp.


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Vector;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;
import org.apache.log4j.Logger;

public class FtpClient{

	private static final Logger logger = Logger.getLogger(FtpClient.class);

	private FTPClient client;

	public FtpClient(boolean ftps) {
		if (ftps) {
			try {
				client = new FTPSClient(true);
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			}
		} else {
			client = new FTPClient();
		}
	}

	public boolean changeDir(String remotePath) throws Exception {
		return client.changeWorkingDirectory(remotePath);
	}

	public boolean connect(String host, String login, String password, int port) throws Exception {
		logger.debug("FTP request connect to " + host + ":" + port);
		client.connect(host, port);
		int reply = client.getReplyCode();
		if (FTPReply.isPositiveCompletion(reply)) {
			logger.debug("FTP request login as " + login);
			if (client.login(login, password)) {
				client.enterLocalPassiveMode();
				return true;
			}
		}
		disconnect();
		return false;
	}

	public void disconnect() throws Exception {
		logger.debug("FTP request disconnect");
		client.disconnect();
	}


	protected boolean downloadFileAfterCheck(String remotePath, String localFile) throws IOException {

		boolean rst;
		FileOutputStream out = null;
		try {
			File file = new File(localFile);
			if (!file.exists()) {
				out = new FileOutputStream(localFile);
				rst = client.retrieveFile(remotePath, out);
			} else {
				rst = true;
			}
		} finally {
			if (out != null) {
				out.close();
			}
		}
		if (out != null) {
			out.close();
		}
		return rst;
	}

	protected boolean downloadFile(String remotePath, String localFile) throws IOException {

		boolean rst;
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(localFile);
			rst = client.retrieveFile(remotePath, out);
		} finally {
			if (out != null) {
				out.close();
			}
		}
		return rst;
	}

	public Vector<String> listFileInDir(String remoteDir) throws Exception {
		if (changeDir(remoteDir)) {
			FTPFile[] files = client.listFiles();
			Vector<String> v = new Vector<String>();
			for (FTPFile file : files) {
				if (!file.isDirectory()) {
					v.addElement(file.getName());
				}
			}
			return v;
		} else {
			return null;
		}
	}

	public boolean uploadFile(String localFile, String remotePath) throws IOException {
		FileInputStream in = new FileInputStream(localFile);
		boolean rst;
		try {
			rst = client.storeFile(remotePath, in);
		} finally {
			in.close();
		}
		return rst;
	}

	@Override
	public Vector<String> listSubDirInDir(String remoteDir) throws Exception {
		if (changeDir(remoteDir)) {
			FTPFile[] files = client.listFiles();
			Vector<String> v = new Vector<String>();
			for (FTPFile file : files) {
				if (file.isDirectory()) {
					v.addElement(file.getName());
				}
			}
			return v;
		} else {
			return null;
		}
	}

	protected boolean createDirectory(String dirName) {
		try {
			return client.makeDirectory(dirName);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}


	public boolean isARemoteDirectory(String path) {
		String cache = "/";
		try {
			cache = client.printWorkingDirectory();
		} catch (NullPointerException e) {
			//nop
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			boolean isDir = changeDir(path);
			changeDir(cache);
			return isDir;
		} catch (IOException e) {
			//e.printStackTrace();
		} catch (Exception e) {
			//e.printStackTrace();
		}
		return false;
	}

	public String getWorkingDirectory() {
		try {
			return client.printWorkingDirectory();
		} catch (IOException e) {
		}
		return null;
	}

}

 


the example of SFTP, the jar supporting this is com.jcraft.jsch


import
import
import
import
import
  
import
import
import
import
  
import
import
import
import
import
import
  
public class
  
private
  
private
  
public
null;  
    }  
  
public boolean connect(String host, String login, String password, int port) throws
  
//If the client is already connected, disconnect
if (command != null) {  
            disconnect();  
        }  
new
try
"no");  
            session = SftpClientFactory.createConnection(host, port, login.toCharArray(), password.toCharArray(), fso);  
"sftp");  
            channel.connect();  
            command = (ChannelSftp) channel;  
  
catch
            e.printStackTrace();  
return false;  
        }  
return
    }  
  
public void
if (command != null) {  
            command.exit();  
        }  
if (session != null) {  
            session.disconnect();  
        }  
null;  
    }  
  
public Vector<String> listFileInDir(String remoteDir) throws
try
            Vector<LsEntry> rs = command.ls(remoteDir);  
new
for (int i = 0; i < rs.size(); i++) {  
if
                    result.add(rs.get(i).getFilename());  
                }  
            }  
return
catch
            e.printStackTrace();  
            System.err.println(remoteDir);  
throw new
        }  
    }  
  
public Vector<String> listSubDirInDir(String remoteDir) throws
        Vector<LsEntry> rs = command.ls(remoteDir);  
new
for (int i = 0; i < rs.size(); i++) {  
if
                result.add(rs.get(i).getFilename());  
            }  
        }  
return
    }  
  
protected boolean
try
            command.mkdir(dirName);  
catch
return false;  
        }  
return true;  
    }  
  
protected boolean downloadFileAfterCheck(String remotePath, String localPath) throws
null;  
try
new
if
new
                command.get(remotePath, outputSrr);  
            }  
catch
try
" not found in "
catch
                e1.printStackTrace();  
            }  
            e.printStackTrace();  
return false;  
finally
if (outputSrr != null) {  
                outputSrr.close();  
            }  
        }  
return true;  
    }  
  
protected boolean downloadFile(String remotePath, String localPath) throws
new
try
            command.get(remotePath, outputSrr);  
catch
try
" not found in "
catch
                e1.printStackTrace();  
            }  
            e.printStackTrace();  
return false;  
finally
if (outputSrr != null) {  
                outputSrr.close();  
            }  
        }  
return true;  
    }  
  
protected boolean uploadFile(String localPath, String remotePath) throws
new
try
            command.put(inputSrr, remotePath);  
catch
            e.printStackTrace();  
return false;  
finally
if (inputSrr != null) {  
                inputSrr.close();  
            }  
        }  
return true;  
    }  
  
public boolean changeDir(String remotePath) throws
try
            command.cd(remotePath);  
catch
return false;  
        }  
return true;  
    }  
  
public boolean
try
return
catch
//e.printStackTrace();
        }  
return false;  
    }  
  
public
try
return
catch
            e.printStackTrace();  
        }  
return null;  
    }  
  
}