jueves, 13 de febrero de 2014

Programa para Descargar archivos desde un FTP

Como estoy haciendo un proyecto de una aplicación para el móvil que descarga directorios de un FTP
he decidido publicar mis avances y si alguno lo lee y sabe quizás puede alguien me ayude.

Aquí esta la librería que lista y descarga el directorio
//    DOwnload Gui User 0.5
//
//
//    This file is part of Dogu.
//    Dogu is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    at your option) any later version.
//
//    Foobar is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Foobar.  If not, see .
//    
//
//    Jaime Velasco Garcia << Salajadin98@gmail.com >>
//  
//    








public class FTPUtil {

public static void downloadDirectory(FTPClient ftpClient, String parentDir,
        String currentDir, String saveDir) throws IOException {
    String dirToList = parentDir;
    if (!currentDir.equals("")) {
        dirToList += "/" + currentDir;
    }

    FTPFile[] subFiles = ftpClient.listFiles(dirToList);

    if (subFiles != null && subFiles.length > 0) {
        for (FTPFile aFile : subFiles) {
            String currentFileName = aFile.getName();
            if (currentFileName.equals(".") || currentFileName.equals("..")) {
                continue;
            }
            String filePath = parentDir + "/" + currentDir + "/"
                    + currentFileName;
            if (currentDir.equals("")) {
                filePath = parentDir + "/" + currentFileName;
            }

            String newDirPath = saveDir + parentDir + File.separator
                    + currentDir + File.separator + currentFileName;
            if (currentDir.equals("")) {
                newDirPath = saveDir + parentDir + File.separator
                          + currentFileName;
            }

            if (aFile.isDirectory()) {
                File newDir = new File(newDirPath);
                boolean created = newDir.mkdirs();
                if (created) {
                    System.out.println("Creado el directorio: " + newDirPath);
                } else {
                    System.out.println("No se pudo crear el directorio: " + newDirPath);
                }

                downloadDirectory(ftpClient, dirToList, currentFileName,
                        saveDir);
            } else {
                boolean success = downloadSingleFile(ftpClient, filePath,
                        newDirPath);
                if (success) {
                    System.out.println("Archivo descargado: " + filePath);
                } else {
                    System.out.println("Error al descargar Archivo: "
                            + filePath);
                }
            }
        }
}

public static boolean downloadSingleFile(FTPClient ftpClient,
        String remoteFilePath, String savePath) throws IOException {
    File downloadFile = new File(savePath);
     
    File parentDir = downloadFile.getParentFile();
    if (!parentDir.exists()) {
        parentDir.mkdir();
    }
         
    OutputStream outputStream = new BufferedOutputStream(
            new FileOutputStream(downloadFile));
    try {
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        return ftpClient.retrieveFile(remoteFilePath, outputStream);
    } catch (IOException ex) {
        throw ex;
    } finally {
        if (outputStream != null) {
            outputStream.close();
        }
    }
}

Y aquí el Main:



//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//
//  Jaime Velasco Garcia << Salajadin98@gmail.com >>

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see .
import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;

public class Dogu {

    public static void main(String[] args) {
        String server = "ponerserver";
        int port = 21;
        String user = "poneruser";
        String pass = "ponerpass";
        String codigo;
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        System.out.print(“Escribe El Codigo Proporcionado: “);
        codigo = br.readLine();
        FTPClient ftpClient = new FTPClient();

        try {
            ftpClient.connect(server, port);
            ftpClient.login(user, pass);

            ftpClient.enterLocalPassiveMode();

            System.out.println("Conectado");

            String remoteDirPath = "Poner Raiz" + codigo;
            String saveDirPath = "Raiz donde Descargar";

            FTPUtil.downloadDirectory(ftpClient, remoteDirPath, "", saveDirPath);

            ftpClient.logout();
            ftpClient.disconnect();

            System.out.println("Desconectado");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}




Ahora estoy aprendiendo a hacer la GUI de android

No hay comentarios:

Publicar un comentario