【问题标题】:Get hidden files in FTP server获取 FTP 服务器中的隐藏文件
【发布时间】:2018-01-10 04:19:08
【问题描述】:

我有本地服务器 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) {}
    }
}

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

【问题讨论】:

  • 你找到答案了吗?

标签: java eclipse ftp


【解决方案1】:

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

#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("..")

就是这样,希望对你有帮助

【讨论】:

  • 我现在没有时间审查这个答案的质量 - 希望它是有用的,但据我所知它的质量很低。致模组:我会尽快让它变得更好,如果有人举报它,请不要在 12 小时内删除它。假设我把它放在我的翅膀下;)
  • 好的,我已经查看并修复了缩进,但我无法真正弄清楚问题的重点,更不用说答案的重点了。 Feci quod potui, faciant meliora potentes。
  • 据我所知,有必要从 FTP 服务器获取隐藏文件,例如在 Python 库中,FTP.nlst() 方法只返回可见文件(没有隐藏文件)并且问题出在其中,即获取名称从'.'开始的(或完整路径)文件
猜你喜欢
  • 1970-01-01
  • 2013-03-20
  • 1970-01-01
  • 1970-01-01
  • 2021-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多