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

时间:2017-06-05 19:46:09

标签: 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服务器中的文件,但受保护的文件如' ._ 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) {}
    }
}

但是认为该方法只检查一次名称,所以如果我再次添加._,它应该可以工作。虽然它没有或我无法以正确的方式应用它。

1 个答案:

答案 0 :(得分:0)

我不知道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("..")

这一切,我希望这将是有用的信息