Created
August 3, 2012 03:15
-
-
Save h3nrique/3244017 to your computer and use it in GitHub Desktop.
Class to do useful things on the FTP server.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.package.utils; | |
/* | |
* This code is free software; you can redistribute it and/or modify it under | |
* the terms of the GNU Lesser General Public License as published by the Free | |
* Software Foundation; either version 2.1 of the License, or (at your option) | |
* any later version. | |
* | |
* This code 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 Lesser General Public License for more | |
* details. | |
* | |
* You should have received a copy of the GNU Lesser General Public License | |
* along with this program; if not, write to the Free Software Foundation, Inc., | |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
*/ | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.net.InetAddress; | |
import java.net.UnknownHostException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.apache.commons.net.ftp.FTP; | |
import org.apache.commons.net.ftp.FTPClient; | |
import org.apache.commons.net.ftp.FTPFile; | |
import org.apache.commons.net.ftp.FTPReply; | |
/** | |
* Class to do useful things on the FTP server. V. 0.1 | |
* | |
* <blockquote> | |
* Dependency: <a href="http://commons.apache.org/net/">commons-net 3.1</a> | |
* </blockquote> | |
* | |
* @author <a href="mailto:paulohenriqueas13@gmail.com">Paulo H3nrique Alves</a> | |
*/ | |
public class FtpUtils { | |
private static void downloadNewFilesFromFtpDirectory(String host, String username, String password, String remoteFolder, List<String> existentFiles, String localStoreFolder) { | |
FTPClient ftpClient = new FTPClient(); | |
InetAddress hostAddr = null; | |
try { | |
hostAddr = InetAddress.getByName(host); | |
} catch (UnknownHostException err) { | |
System.err.println("Host inválido."); | |
System.exit(1); | |
} catch (Exception err) { | |
System.err.println("Erro genérico :: " + err.getMessage()); | |
System.exit(1); | |
} | |
try { | |
int hostReply; | |
ftpClient.connect(hostAddr); | |
System.out.print(ftpClient.getReplyString()); | |
hostReply = ftpClient.getReplyCode(); | |
if (!FTPReply.isPositiveCompletion(hostReply)) { | |
ftpClient.disconnect(); | |
System.err.println("Servidor FTP recusando conexões."); | |
System.exit(1); | |
} | |
if (ftpClient.login(username, password)) { | |
System.out.print(ftpClient.getReplyString()); | |
ftpClient.setFileType(FTP.BINARY_FILE_TYPE); | |
System.out.print(ftpClient.getReplyString()); | |
ftpClient.enterLocalPassiveMode(); | |
if (ftpClient.changeWorkingDirectory(remoteFolder)) { | |
System.out.print(ftpClient.getReplyString()); | |
System.out.println("Listando arquivos do diretório."); | |
FTPFile[] files = ftpClient.listFiles(); | |
System.out.print(ftpClient.getReplyString()); | |
List<String> filesToDownload = new ArrayList<String>(); | |
for (FTPFile ftpFile : files) { | |
if (ftpFile.isFile()) { | |
boolean download = true; | |
for (String existentFile : existentFiles) { | |
if (ftpFile.getName().equals(existentFile)) { | |
download = false; | |
} | |
} | |
if (download) { | |
filesToDownload.add(ftpFile.getName()); | |
} | |
} | |
} | |
for (String filename : filesToDownload) { | |
String filenameDownload = localStoreFolder + File.separator + filename; | |
File downloadFile = new File(filenameDownload); | |
FileOutputStream fileOutputStream = null; | |
try { | |
fileOutputStream = new FileOutputStream(downloadFile); | |
} catch (Exception err) { | |
System.out.println("Diretório local não existe :: " + localStoreFolder); | |
System.exit(1); | |
} | |
System.out.println("Baixando arquivo :: " + filename); | |
ftpClient.retrieveFile(filename, fileOutputStream); | |
System.out.print(ftpClient.getReplyString()); | |
} | |
} else { | |
System.out.println("Diretório não existe :: " + remoteFolder); | |
} | |
ftpClient.logout(); | |
System.out.print(ftpClient.getReplyString()); | |
} | |
} catch (Exception err) { | |
err.printStackTrace(); | |
} finally { | |
if (ftpClient.isConnected()) { | |
try { | |
ftpClient.disconnect(); | |
System.out.println("Desconectado do servidor :: " + hostAddr); | |
} catch (Exception err) { | |
} | |
} | |
} | |
} | |
public static void main(String[] args) { | |
List<String> existentFiles = new ArrayList<String>(); | |
existentFiles.add("arquivo1.txt"); | |
existentFiles.add("arquivo2.txt"); | |
existentFiles.add("arquivo3.txt"); | |
FtpUtils.downloadNewFilesFromFtpDirectory("www.site.com.br", "username", "password", "/www", existentFiles, "/home/user/Desktop"); // Linux | |
// FtpUtils.downloadNewFilesFromFtpDirectory("www.site.com.br", "username", "password", "/www", existentFiles, "c:\temp"); // Windows | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment