drapostolos / rdp4j

Remote Directory Poller for Java
MIT License
47 stars 25 forks source link

Can't Monitor directory when file or folder change #25

Closed shihua-guo closed 3 years ago

shihua-guo commented 3 years ago

I follow the guide , but when program start up, just print 'initial Content: ^', and I try to add/remove some files, nothing show up.

shihua-guo commented 3 years ago

And I found the problem, is about the FTPclient ,When I monitor the sub directory such as /data/vsftpd/microSource/download listFiles can't list the file or foleder of this directory . Below is the comment of org.apache.commons.net.ftp.FTPClient#listFiles method:

     * Using the default system autodetect mechanism, obtain a
     * list of file information for the current working directory
     * or for just a single file.

Notice listFiles is list current working directory, turn to the FtpDirectorysample code :

ftp = new FTPClient();
ftp.connect(host);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
    ftp.disconnect();
    throw new IOException("Exception when connecting to FTP Server: " + ftp);
}
ftp.login(username, password);

Set<FileElement> result = new LinkedHashSet<FileElement>();
for(FTPFile file : ftp.listFiles(workingDirectory)){
    result.add(new FtpFile(file));
}

when ftp login, it's seem that the default directory is home directory . So When List File , We need to invoke changeWorkingDirectory. And I can monitor the change of directory when I do as follow (just add a line of code ftp.changeWorkingDirectory(workingDirectory);)

ftp = new FTPClient();
ftp.connect(host);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
    ftp.disconnect();
    throw new IOException("Exception when connecting to FTP Server: " + ftp);
}
ftp.login(username, password);
ftp.changeWorkingDirectory(workingDirectory);
Set<FileElement> result = new LinkedHashSet<FileElement>();
for(FTPFile file : ftp.listFiles()){
    result.add(new FtpFile(file));
}
drapostolos commented 3 years ago

Thanks! I'll update the guide with this 👍