1

I have the local server mockftpserver, and in the server there are couple of files and they are protected with a prefix '._' and the method to protect from getting those files is the following :

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("._", "");
}

I tried to list the files in the FTP server I got them but the protected ones like '._passwrd' I was not able to see it. I used the normal method to get the file list:

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

But thinking that the method will only check the name once, so if I added the ._ once again it should work. Although it did not or I could not apply it in the right way.

Filip Malczak
3,2221 gold badge26 silver badges45 bronze badges
asked Jun 5, 2017 at 19:46
1
  • did you find answer ? Commented Jan 9, 2018 at 13:41

2 Answers 2

0

i don't know about Java but in Python i solved similar task in the following way:
i used: FTP server, Python 2.7.12, library 'ftplib'
so i show just needed part with comments:

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

that all, i hope it will be helpful information

Filip Malczak
3,2221 gold badge26 silver badges45 bronze badges
answered Jan 9, 2018 at 15:17
Sign up to request clarification or add additional context in comments.

3 Comments

I don't have time to review the quality of this answer right now - hopefully it is useful, but as far as I can see it's low quality. To mods: I'll try to make it better ASAP, if someone flags it, please don't remove it for 12h. Let's say that I take it under my wings ;)
OK, I've reviewed it and fixed indendation, but I cannot really figure out the point of the question, not even to mention the point of the answer. Feci quod potui, faciant meliora potentes.
as I understood it was necessary to get hidden files from FTP server, for examlpe in Python lib, FTP.nlst() method returns only visible file (without hidden files) and problem was in it, i.e. get name (or full path) files which started from '.'
0

You should set your ftpClient to list hidden Files before listing the files, so:

ftpClient.setListHiddenFiles(true);
FTPFile[] files = ftpClient.listFiles(); 
answered Jun 23, 2022 at 14:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.