gpt4 book ai didi

android - 如何检查FTP服务器上是否存在文件?

转载 作者:搜寻专家 更新时间:2023-11-01 09:12:17 25 4
gpt4 key购买 nike

我在 android 上使用 apache FTPClient。我想从 ftp 服务器下载一个文件。但是我想在下载之前检查它是否存在于服务器上。我该如何检查?

谢谢,

我的代码:

public static boolean getFile(String serverName, String userName,
String password, String serverFilePath, String localFilePath)
throws Exception {

FTPClient ftp = new FTPClient();
try {
ftp.connect(serverName);
int reply = ftp.getReplyCode();

if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return false;
}
} catch (IOException e) {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException f) {
throw e;
}
}
throw e;
} catch (Exception e) {
throw e;
}

try {
if (!ftp.login(userName, password)) {
ftp.logout();
}
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();

OutputStream output;

output = new FileOutputStream(localFilePath);
ftp.retrieveFile(serverFilePath, output);
output.close();

ftp.noop(); // check that control connection is working OK
ftp.logout();
return true;

} catch (FTPConnectionClosedException e) {
throw e;
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw e;
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException f) {
throw f;
}
}

}

}

最佳答案

String[] files = ftp.listnames();

查找文件,如果需要的文件名是include...

关于android - 如何检查FTP服务器上是否存在文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7415373/

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