2017-06-05 142 views
1

我有本地服务器mockftpserver,并且在服务器中有几个文件,并且它们使用前缀'._'进行保护,并且保护获取这些文件的方法如下:获取FTP服务器中的隐藏文件

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) {} 
    } 
} 

但认为该方法将只检查一次名,所以如果我增加了._再次它应该工作。虽然它没有或者我不能以正确的方式应用它。

+0

你找到答案吗? – Vadim

回答

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

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

+0

我现在没有时间回顾此答案的质量 - 希望它有用,但据我所见,它的质量很差。对于mods:我会尽力让它更好,如果有人标记它,请不要在12小时内删除它。假设我把它放在我的翅膀下;) –

+0

好吧,我已经检查了它并修正了indendation,但我无法弄清楚问题的重点,甚至没有提到答案的要点。 Feci quod potui,相当meliora潜力。 –

+0

据我所知,有必要从FTP服务器获取隐藏文件,对于Python lib中的examlpe,'FTP.nlst()'方法只返回可见文件(没有隐藏文件)并且存在问题,即获取名称(或全名路径)从'。'开始的文件。 – Vadim