2

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;
            }
        }

    }

}
4

4 に答える 4

3
String[] files = ftp.listnames();

必要なファイル名がインクルードされている場合、ファイルを検索します...

于 2012-06-13T13:50:33.837 に答える
2

ftpClientが のインスタンスであると仮定しますorg.apache.commons.net.ftp.FTPClient:

public boolean fileExists(String fileName) throws IOException
{
    String[] files = ftpClient.listNames();

    return Arrays.asList(files).contains(fileName);
}
于 2015-06-15T19:08:46.110 に答える
0

クライアントがRETRを送信し、サーバーがエラーコード550で応答した場合、ファイルが存在しないか、ファイルを取得する権限がないことを確信できます... FTP仕様は少し緩いので、あなたはただ仮定するかもしれません永続的なファイルシステム エラーを示す 550 ~ 559 の範囲のエラー。

于 2011-09-14T15:21:01.637 に答える