如何在java中从ftp服务器读取多个文件?
我有如下ftp服务器如何在java中从ftp服务器读取多个文件?,java,ftp,Java,Ftp,我有如下ftp服务器 MainFolder -Location1 -download.txt -otherFiles... -Location2 -download.txt -otherFiles... -Location3 -download.txt -otherFiles... -LocationN -download.txt -otherFiles... 我想从每个位置文件夹中读取download.txt文件并插入数据库 我写了下
MainFolder
-Location1
-download.txt
-otherFiles...
-Location2
-download.txt
-otherFiles...
-Location3
-download.txt
-otherFiles...
-LocationN
-download.txt
-otherFiles...
我想从每个位置文件夹中读取download.txt
文件并插入数据库
我写了下面的代码,但它只读取Location1
文件夹中的download.txt
文件,而不读取其余位置文件夹中的其他download.txt
文件
我的代码有什么问题
下面是我的代码
public class ReadFtpFile {
static Logger logger = Logger.getLogger(ReadFtpFile.class);
public static String MAIN_FOLDER_PATH = "MAIN_FOLDER";
public static String fileNameToBeRead = "download";
public static void main(String[] args) {
String ftpUrl = "10.x.x.x";
String user = "username";
String pass = "password";
FTPFile[] listOfFolders =null;
int location = 0;
FTPFile[] listOfFiles = null;
// get an ftpClient object
FTPClient ftpClient = new FTPClient();
// pass directory path on server to connect
ftpClient.connect(ftpUrl);
// pass username and password, returned true if authentication is successful
boolean login = ftpClient.login(user, pass);
FTPFileFilter filter = new FTPFileFilter() {
@Override
public boolean accept(FTPFile ftpFile) {
return (ftpFile.isFile() && ftpFile.getName().contains(fileNameToBeRead));
}
};
if (login) {
logger.info("Connection established...");
FTPFile[] locationfolders = ftpClient.listDirectories(MAIN_FOLDER_PATH);
for (FTPFile folder : locationfolders) {
if(folder.isDirectory()){
logger.info("Folder Found : " + folder.getName());
String subFolder = MAIN_FOLDER_PATH + "/" +folder.getName();
FTPFile[] subfiles = ftpClient.listFiles(subFolder,filter);
for(FTPFile myfile : subfiles){
logger.info("File Name: " + myfile.getName());
BufferedReader in = new BufferedReader(new InputStreamReader(ftpClient.retrieveFileStream(subFolder+ "/" +myfile.getName())));
logger.info("Reading file start.");
String inputLine;
while ((inputLine = in.readLine()) != null){
// logger.info(inputLine);
}
in.close();
logger.info("File Reading Completed");
}
}
}
// logout the user, returned true if logout successfully
boolean logout = ftpClient.logout();
if (logout) {
logger.info("Connection close...");
}
} else {
logger.info("Connection fail...");
}
}
}
输出:
Connection established...
22-05-2017 10:54:49 INFO ReadFtpFile:74 - Folder Found : Location1
22-05-2017 10:54:49 INFO ReadFtpFile:78 - File Name: download.txt
22-05-2017 10:54:49 INFO ReadFtpFile:80 - Reading file start.
22-05-2017 10:54:49 INFO ReadFtpFile:87 - File Reading Completed
22-05-2017 10:54:49 INFO ReadFtpFile:74 - Folder Found : Location2
22-05-2017 10:54:49 INFO ReadFtpFile:74 - Folder Found : Location3
22-05-2017 10:54:49 INFO ReadFtpFile:74 - Folder Found : LocationN
定义“不读取”。它的作用是什么?@EJP我更新了问题。它只读取第一个文件download.txt,而不读取其他文件。您只是检查一个文件夹,然后从if语句中出来。在退出if语句之前,您需要遍历所有文件夹。@Amarnath我正在遍历子文件夹以及我要退出if语句的地方?哦,我的错。你在循环。。抱歉,错过了顶部的for循环。请定义“不读取”。它的作用是什么?@EJP我更新了问题。它只读取第一个文件download.txt,而不读取其他文件。您只是检查一个文件夹,然后从if语句中出来。在退出if语句之前,您需要遍历所有文件夹。@Amarnath我正在遍历子文件夹以及我要退出if语句的地方?哦,我的错。你在循环。。抱歉错过了顶部的for循环。