假设,FTP位置中有100个文件。每次调用该函数时,我要下载100个文件中的20个文件。调用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";
}
答案 0 :(得分:2)
据我了解,您想在首次通话中下载前20个项目。在第二次通话中,您要下载20至40项,就像这样。
FTPFile[] files = ftpClient.listFiles();
此方法列出了FTP中的所有项目。因此,您必须选择要下载的项目。
我的建议是您可以像这样调用函数;
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
例如,您可以传递索引值= 0、20、40、60、80;
for(int i = 0;i<5;i++){
read(userName,password,ftpDirectory,downloadDir,i*20);
}
答案 1 :(得分:0)
您可以使用 iteration变量,例如G。 I,每次下载特定文件时,您计算i加一个,如果 i> = 20 则中止该功能。