java - 获取FTP服务器中的隐藏文件

标签 java eclipse ftp

我有本地服务器mockftpserver,在服务器中有几个文件,它们受到前缀“._”的保护,防止获取这些文件的方法如下:

protected String getRealPath(Session session, String path) {
    String currentDirectory = (String) session.getAttribute(SessionKeys.CURRENT_DIRECTORY);
    String result;
    if (path == null) {
        result = currentDirectory;
    }
    else if (getFileSystem().isAbsolute(path)) {
        result = path;
    }
    else {
        result = getFileSystem().path(currentDirectory, path);
    }
    return result.replace("._", "");
}

我尝试列出 FTP 服务器中的文件,但我无法看到 protected 文件,例如“._passwrd”。 我使用普通方法获取文件列表:

boolean login = ftpClient.login("user", "password"); 

if (login) {  
    System.out.println("Connection established...");  
    FTPFile[] files = ftpClient.listFiles();  

    for (FTPFile file : files) {  
        if (file.getType() == FTPFile.FILE_TYPE) {  
            System.out.println("File Name: "  
            + file.getName()  
            + " File Size: " );  
        }  
    }  
    String[] fil = ftpClient.listNames();
    if (files != null && fil.length > 0) {
        for (String aFile: fil) {
            System.out.println(aFile);
        }
    }
    BufferedReader reader = null;
    String firstLine = null;

    try {
        InputStream stream = 
            ftpClient.retrieveFileStream("._"+"._passwd");
        reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
        firstLine = reader.readLine();
    } finally {
        if (reader != null) 
            try { 
                reader.close(); 
            } catch (IOException logOrIgnore) {}
    }
}

但认为该方法只会检查名称一次,因此如果我再次添加 ._ ,它应该可以工作。尽管它没有或者我无法以正确的方式应用它。

最佳答案

我不了解 Java,但在 Python 中我通过以下方式解决了类似的任务:
我使用:FTP 服务器,Python 2.7.12,库“ftplib”
所以我用评论展示了只需要的部分:

#while customer list not empty
while self.customerDirs:
    #create connect to root
    self.connection.cwd("/")
    #choose customer
    customer = self.customerDirs.pop()
    try:
        #go inside to customer's folder
        self.connection.cwd(customer)
        #for all folders inside
        for dir in self.connection.nlst():
        #go inside
        self.connection.cwd(dir)
        #create empty list
        hiddenList = []
        #create variable which contains path
        pathDir = self.connection.pwd()
        #write to list hidden files
        self.connection.retrlines("LIST -a", hiddenList.append)
        for entry in hiddenList:
            #split value and take file name
            entrySplit = entry.split(' ')[-1]
            #cheсk file name
            if entrySplit not in ['.', '..'] and entrySplit.startswith('.'):
                #all necessary files are sent to method which will delete it (lool like: /customer/folder/.hidden_file.hid)
                self.ftp_delete_file('/'.join([pathDir, entrySplit]))
                #return to step up
            self.connection.cwd("..")

这就是全部,我希望这会是有用的信息

关于java - 获取FTP服务器中的隐藏文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44376558/

相关文章:

java - 背包但确切重量

java - volatile 数组的替代品

android - R 无法解析为变量 |找不到 [YourAndroiProjectName].apk

java - %trimmedwindow.label.eclipseSDK 作为标题栏文本 : Bug 373988

php - 带有MLSD和fsockopen的PHP的ftp_raw函数: weird blocking behaviour and Warning messages

java - FTP 还是其他方式?我只需要指导

java - Hibernate slf4j异常

java - 什么是 NullPointerException,我该如何解决?

java - 无法在 Eclipse-Java 中编译/运行

php - WKHTMLTOPDF 抛出错误代码 127