在FTP服务器中获取隐藏文件 [英] Get hidden files in FTP server
问题描述
我拥有本地服务器mockftpserver,并且在服务器中有几个文件,它们使用前缀'._'进行保护,并且防止获取这些文件的方法如下所示:
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("._", "");
}
我试图列出FTP服务器中的文件,诸如'._passwrd'之类的我无法看到它。
我使用普通的方法来获取文件列表:
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.
推荐答案
我不知道Java的问题,但是在Python中我用下面的方法解决了类似的问题:
i使用:FTP服务器,Python 2.7.12,library'ftplib'
所以我只显示需要部分的注释:
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
这篇关于在FTP服务器中获取隐藏文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!