Posts tagged ‘java FTP’

julio 18, 2010

Conectar a un servidor FTP desde java

Ejemplo de como establecer conexión a un servidor FTP.

Paquete

import java.io.IOException;
import java.net.InetAddress;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
/**
 * @author Leyer
 */
public class FTP extends FTPClient{
	private String username = null;
	private String password = null;
	private String host     = null;
	public FTP(String host,String username,String password){
		this.username = username;
		this.password = password;
		this.host     = host;
		try {
			connect(InetAddress.getByName(this.host));
			boolean login=login(username, password);
				if(login){
					System.out.println("Host: "+host);
					System.out.println("Login success...");
					 FTPFile[] ftpFiles = this.listFiles();
					    for (FTPFile ftpFile : ftpFiles) {
					        System.out.println("FTPFile: " + ftpFile.getName() + "; "+ ftpFile.getSize());	      
					    }
				}else
				System.out.println("Logout from FTP server...");
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
	}
	public String getUsername() {return username;	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {return password;}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getHost() {return host;}
	public void setHost(String host) {
		this.host = host;
	}
	public static void main(String[] args) {
		new FTP("host","XXXX", "XXXXX");
	}
}
Etiquetas: