すきま風

勉強したことのメモとか

Spring Integration Ftpのexists メソッドがどんなときもTrueを返す件について

なんてアホな子なのだ!

調べた

DefaultのExists ModeはFTP STAT なのでここで判定している。

https://github.com/spring-projects/spring-integration/blob/main/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/session/FtpRemoteFileTemplate.java#L87-L88

   public boolean exists(final String path) {
        return doExecuteWithClient(client -> {
            try {
                switch (FtpRemoteFileTemplate.this.existsMode) {

                    case STAT:
                        return client.getStatus(path) != null;

ここで使われる client は apache.common.net.ftp なのだが、このclient.getStatus(path) はファイルが存在しない場合でもFTP Statusを文字列として返してしまう。

ファイルがある場合

213-STAT\r\n-rw-r--r--    1 1000       ftpgroup            0 Dec 12 15:32 /foo/bar.txt\r\n213 End.

ファイルがない場合

213-STAT\r\n213 End.

ファイルがない場合でも文字列が返るので、どんなときも existsはTrueを返してくる。イエスマンかな?

じゃあどうすればいいの

Exists Mode を NLST にしてあげれば正しく動作する。

val template = FtpRemoteFileTemplate(sessionFactory)
template.setExistsMode(FtpRemoteFileTemplate.ExistsMode.NLST)

// 正しく動作する
template.exists(path)