gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:25:50 25 4
gpt4 key购买 nike

我有本地服务器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 服务器中的文件,但我无法看到 protected 文件,例如“._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,但在 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("..")

这就是全部,我希望这会是有用的信息

关于java - 获取FTP服务器中的隐藏文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44376558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com