[英]Preventing fetching the same file from FTP server using multiple FTP clients in Java
[英]how to get list of file from ftp server if server is very slow or there are more clients are there in java
我必須獲取ftp服務器中存在的文件或文件夾的列表。我嘗試了以下代碼,但沒有得到該列表。 如果我運行相同的代碼,如果我將本地系統作為服務器,則將獲取文件列表,但是當我應用相同的代碼時,我將成功登錄並且FTPClient
類的列表方法正在工作,但listFiles()
, listNames()
無效。
import java.io.IOException;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.io.FileUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
/**
*
* @author admin
*/
public class ListFileOfServerCheck {
public static void main(String args[]) throws SocketException, IOException{
FTPClient client = new FTPClient();
ArrayList dirList=new ArrayList();
client.connect("myserver");
client.login("vishal", "vishal");
if(client.isConnected())
System.out.println("connected");
int i=0;
System.out.println("number of files on server="+client.list());
String[] names = null;
int itration=1;
while(names==null){
System.out.println(itration +"th itration");
names = client.listNames();
itration++;
}
client.logout();
client.disconnect();
}
}
問題是登錄FTP時當前的目錄是什么。 嘗試使用:
client.listNames("/path/to/the/folder");
而不是依賴當前的工作目錄。
最好檢查一下返回的結果
client.login(...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.