[英]How to download first 20 files from ftp using ftpclient java
Assume, there are 100 files in a FTP location.假设一个 FTP 位置中有 100 个文件。 I want to download 20 files out of 100 each time I call the function.每次调用该函数时,我想下载 100 个文件中的 20 个。 How i can achieve that when i call the readFTP function?当我调用 readFTP 函数时如何实现?
ReadFTPread(String host, String userName, String password,String ftpDirectory,String downloadDir)
public static String read(String host, String userName, String password,String ftpDirectory,String downloadDir) {
Logger logger = Logger.getLogger("LCAlertLog");
// get an ftpClient object
FTPClient ftpClient = new FTPClient();
FileOutputStream fos = null;
String fileName = "";
try {
ftpClient.connect(host);
//boolean login = ftpClient.login(username,password);
boolean login = ftpClient.login(userName, password);
if (login) {
//Logger.info(Connection established.####..");
ftpClient.changeWorkingDirectory(ftpDirectory);
int returnCode = ftpClient.getReplyCode();
logger.info("### FTP RETURN CODE for change to: "+ ftpDirectory+" :"+returnCode);
////System.out.println("### FTP RETURN CODE for change to: "+ ftpDirectory+" :"+returnCode);
FTPFile[] files = ftpClient.listFiles();
for (FTPFile file : files) {
if (file.getType() == FTPFile.FILE_TYPE) {
logger.info("File Name: "+ file.getName());
fileName = file.getName();
Date lastModified = file.getTimestamp().getTime();
Calendar today = Calendar.getInstance();
// Subtract 1 day to get yesterday
// today.add(Calendar.DATE, -8);
today.add(Calendar.DATE, -0); // TODAYS DATE , put -1 to
// get YESTERDAY
Date yesterday = new java.sql.Date(
today.getTimeInMillis());
int flag = 0;
int searchDateRange = 1;
if (searchDateRange == 1) {
fos = new FileOutputStream(downloadDir + fileName);
boolean download = ftpClient.retrieveFile(fileName,fos);
logger.info("#### IS DOWNLOAD: "+download);
////System.out.println("#### IS DOWNLOAD: "+download);
if (download) {
String existingFilepath = ftpDirectory;
String newFilepath = "backup/";
//ftpClient.makeDirectory(newFilepath)
// //System.out.println("### FILE Current: "+existingFilepath+fileName+"### FILE NEW :"+newFilepath+fileName);
/* ftpClient.rename(existingFilepath+fileName,newFilepath+fileName);*/
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ftpClient.retrieveFile(fileName, outputStream);
InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
//now, store this stream to backup directory. First we need to change working directory to backup directory.
// assuming backup directory is with in current working directory
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);//binary files
ftpClient.changeWorkingDirectory(newFilepath);
returnCode = ftpClient.getReplyCode();
logger.info("### FTP RETURN CODE for : "+newFilepath+" :"+returnCode);
////System.out.println("### FTP RETURN CODE for : "+newFilepath+" :"+returnCode);
if (returnCode == 550) {
logger.warn("### Change dir failed return code"+returnCode);
////System.out.println("Change dir failed");
}
//this overwrites the existing file
logger.info("#### FTP CLIENT STORE FILE: "+ftpClient.storeFile(fileName, is));
////System.out.println("#### FTP CLIENT STORE FILE: "+ftpClient.storeFile(fileName, is));
ftpClient.changeWorkingDirectory("../");
logger.info("### FTP RETURN CODE for Previous Dir :"+returnCode);
////System.out.println("### FTP RETURN CODE for Previous Dir :"+returnCode);
ftpClient.deleteFile(fileName);
returnCode = ftpClient.getReplyCode();
logger.info("### FTP RETURN CODE for delete : "+ftpDirectory + fileName+" :"+returnCode);
// //System.out.println("### FTP RETURN CODE for delete : "+ftpDirectory + fileName+" :"+returnCode);
//if you don't want to overwrite it use storeUniqueFile
fos.close();
}
} else if (searchDateRange == 0) {
}
}
}
// logout the user, returned true if logout successfully
boolean logout = ftpClient.logout();
if (logout) {
// //Logger.info(Connection close...");
}
} else {
logger.warn("Connection failed ");
}
// testing.closeFile();
} catch (SocketException e) {
logger.warn("EXCEPTION ",e);
return "Socket Exception";
} catch (IOException e) {
logger.warn("EXCEPTION ",e);
return "IOException";
} catch (Exception e) {
logger.warn("EXCEPTION ",e);
return "exception";
}
return "Success";
}
As I understand, you want to download first 20 items in first call.据我了解,您想在第一次通话中下载前 20 个项目。 In second call you want to download 20th to 40th items and it goes like that.在第二次调用中,您想下载第 20 到 40 个项目,就像这样。
FTPFile[] files = ftpClient.listFiles();
this method is list all items in FTP.此方法是列出 FTP 中的所有项目。 So you have to pick the items that you want to download.因此,您必须选择要下载的项目。
My advice you can call the function like ;我的建议是你可以像这样调用函数;
public static String read(String host, String userName, String password,String ftpDirectory,String downloadDir, int index) {
...
for (int i = index; i<index+20; i++) {
FTPFile file = files[index];//you have the file
You can pass the index values = 0,20,40,60,80 for example;例如,您可以传递索引值 = 0,20,40,60,80;
for(int i = 0;i<5;i++){
read(userName,password,ftpDirectory,downloadDir,i*20);
}
您可以使用迭代变量,例如 I,每次下载特定文件时,您都将 i 加一,如果i >= 20 ,则中止该函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.