package FTP;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
/**
*
* @author ahmateu
*/
public class FTP {
/**
*
@param ftp es la instanci para la calse de FTP
*/
public static FTPClient ftp = null;
private static FileInputStream fis = null;
private static FileOutputStream fos = null;
private static String ip = "";
private static String user = "";
private static String pass = "";
private static String localFile = "";
private static String hostFile="";
private static int tiempo = 0;
/**
*
* @return
*/
public static int getTiempo() {
return tiempo;
}
/**
*
* @param tiempo
*/
public static void setTiempo(int tiempo) {
FTP.tiempo = tiempo;
}
/**
*
* @return
*/
public static String getHostFile() {
return hostFile;
}
/**
*
* @param hostFile
*/
public static void setHostFile(String hostFile) {
FTP.hostFile = hostFile;
}
/**
*
* @return
*/
public static String getIp() {
return ip;
}
/**
*
* @param ip
*/
public static void setIp(String ip) {
FTP.ip = ip;
}
/**
*
* @return
*/
public static String getLocalFile() {
return localFile;
}
/**
*
* @param localFile
*/
public static void setLocalFile(String localFile) {
FTP.localFile = localFile;
}
/**
*
* @return
*/
public static String getPass() {
return pass;
}
/**
*
* @param pass
*/
public static void setPass(String pass) {
FTP.pass = pass;
}
/**
*
* @return
*/
public static String getUser() {
return user;
}
/**
*
* @param user
*/
public static void setUser(String user) {
FTP.user = user;
}
/**
*
* @throws SocketException
* @throws IOException
*/
public static void conectar() throws SocketException, IOException{
ftp = new FTPClient();
ftp.connect(ip);
if(ftp.login(user, pass)){
System.out.println("login OK");
}else
System.out.println("login Error");
}
/**
*
*/
public static void desconectar(){
try {
ftp.logout();
ftp.disconnect();
System.out.println("Googbye!");
} catch (IOException ex) {
// Logger.getLogger(FTP.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("||||||||||||Error en el Envio|||||||||||||");
}
}
/**
*
* @throws IOException
*/
public static void directorioActual() throws IOException{
System.out.println( ftp.printWorkingDirectory());
}
/**
*
* @param dir
* @throws IOException
*/
public static void cambiarDeDirectorioEnFTP(String dir) throws IOException{
ftp.changeWorkingDirectory(dir);
}
/**
*
* @throws IOException
*/
public static void cambiarDirectorioAnterior() throws IOException{
ftp.changeToParentDirectory();
}
/**
*
* @return
* @throws FileNotFoundException
*/
public static boolean enviarArchivoFTP() throws FileNotFoundException{
try {
fis = new FileInputStream(localFile);
if(ftp.storeFile(hostFile,fis)){
System.out.println("send OK");
fis.close();
return true;
}else{
System.out.println("send ERROR");
fis.close();
return false;
}
} catch (IOException ex) {
Logger.getLogger(FTP.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Error en el Envio");
return false;
}
}
/**
*
* @param localFile
* @param hostFile
* @throws FileNotFoundException
* @throws IOException
*/
public static void descargarArchivoFTP(String localFile, String hostFile) throws FileNotFoundException, IOException{
fos = new FileOutputStream(localFile);
if(ftp.retrieveFile("/" + hostFile, fos)){
System.out.println("Desgarca correcta");
}else
System.out.println("Error Descarga");
fos.close();
}
/**
*
* @throws IOException
*/
public static void eliminarArchivoFTP() throws IOException{
boolean deleted = ftp.deleteFile(hostFile);
if (deleted) {
System.out.println("File deleted...");
}else
System.out.println("Error al borrar");
}
/**
*
* @throws IOException
*/
public static void listaArchivos() throws IOException{
FTPFile[] ftpFiles = ftp.listFiles();
for (FTPFile ftpFile : ftpFiles) {
// Check if FTPFile is a regular file
if (ftpFile.getType() == FTPFile.FILE_TYPE) {
System.out.println("FTPFile: " + ftpFile.getName() + "; " + ftpFile.getSize() + " b");
}
}
}
/**
*
* @throws IOException
*/
public static void listaDirectorios() throws IOException{
FTPFile[] ftpFiles = ftp.listDirectories();
for (FTPFile ftpFile : ftpFiles) {
// Check if FTPFile is a regular file
if (ftpFile.getType() == FTPFile.FILE_TYPE) {
// System.out.println("FTPFile: " + ftpFile.getName() + "; " + ftpFile.getSize()/1024 + " Kbs");
System.out.println("FTPFile: " + ftpFile.getName() + "; " + ftpFile.getSize() + " b");
}
}
}
/**
*
* @throws IOException
*/
public static void lista() throws IOException{
String[] names = ftp.listNames();
for (String name : names) {
System.out.println("Name = " + name);
}
}
/**
*
* @throws SocketException
*/
public static void setTimeOut() throws SocketException{
ftp.setSoTimeout(getTiempo());
}
/**
*
* @throws SocketException
*/
public static void getTimeout() throws SocketException{
System.out.println(ftp.getSoTimeout());
}
/**
*
*/
public static void setDataTimeOut(){
ftp.setDataTimeout(tiempo);
}
/**
*
*/
public static void getDataTimeOut(){
System.out.println(ftp.getDataConnectionMode());
}
/**
*
* @throws IOException
*/
public static void mensaje() throws IOException{
System.out.println( ftp.getControlKeepAliveTimeout());
}
}
By Armando Mateu
No hay comentarios:
Publicar un comentario