【问题标题】:FtpSession.exist(path) doesn't work with filesFtpSession.exist(path) 不适用于文件
【发布时间】:2021-04-09 11:54:12
【问题描述】:

我正在尝试使用 Spring Integration 检查 FTP 服务器中的文件是否存在,但它似乎不起作用。它适用于目录但不适用于文件。在文档中提到适用于目录和文件。我是否正确构建了路径?

private DefaultFtpSessionFactory getFTPFactory() {
    DefaultFtpSessionFactory defaultFtpSessionFactory = new DefaultFtpSessionFactory();
    defaultFtpSessionFactory.setHost("localhost");
    defaultFtpSessionFactory.setPort(21);
    defaultFtpSessionFactory.setUsername("foo");
    defaultFtpSessionFactory.setPassword("pass");

    return defaultFtpSessionFactory;
  }




public boolean getFTPFiles() throws IOException {
    DefaultFtpSessionFactory defaultFtpSessionFactory = getFTPFactory();
    FtpSession ftpSession = defaultFtpSessionFactory.getSession();
    FTPClient clientInstance = ftpSession.getClientInstance();
    clientInstance.enterLocalPassiveMode();

    return ftpSession.exists("/ftp/foo/study/download/test_1.txt");
  }

【问题讨论】:

    标签: ftp spring-integration spring-integration-ftp


    【解决方案1】:

    我们不知道您的路径是否正确。 FtpSession.exists()中的逻辑是这样的:

    public boolean exists(String path) throws IOException {
        Assert.hasText(path, "'path' must not be empty");
    
        String[] names = this.client.listNames(path);
        boolean exists = !ObjectUtils.isEmpty(names);
    
        if (!exists) {
            String currentWorkingPath = this.client.printWorkingDirectory();
            Assert.state(currentWorkingPath != null,
                    "working directory cannot be determined; exists check can not be completed");
    
            try {
                exists = this.client.changeWorkingDirectory(path);
            }
            finally {
                this.client.changeWorkingDirectory(currentWorkingPath);
            }
    
        }
    
        return exists;
    }
    

    因此,它会尝试在用户工作目录中列出提供的路径。 如果不是,它会尝试将工作目录切换到提供的路径,这显然不适用于文件名......

    我可以建议尝试使用FtpRemoteFileTemplate.exists()。查看它的 JavaDocs:

    /**
     * This particular FTP implementation is based on the {@link FTPClient#getStatus(String)}
     * by default, but since not all FTP servers properly implement the {@code STAT} command,
     * the framework internal {@link FtpRemoteFileTemplate} instances are switched to the
     * {@link FTPClient#listNames(String)} for only files operations.
     * <p> The mode can be switched with the {@link #setExistsMode(ExistsMode)} property.
     * <p> Any custom implementation can be done in an extension of the {@link FtpRemoteFileTemplate}.
     * @param path the remote file path to check.
     * @return true or false if remote file exists or not.
     */
    @Override
    public boolean exists(final String path) {
    

    我不确定localPassive/ActiveMode...

    也许有一种方法可以调查服务器日志以确定为什么它不允许您查看该文件的问题?

    【讨论】:

    • 实际上服务器允许我查看该文件。如果我执行 ftpSession.list("/ftp/foo/study/download") 我实际上可以在 FTPFile[] 中看到我返回的文件。我还可以使用 ftpSession.read("/ftp/foo/study/download/test_1.txt", dummyStream) 读取文件。只是存在不起作用,这很奇怪。
    • 正如我所说:参见FtpRemoteFileTemplate.exists()JavaDocs。并非所有 FTP 服务器都允许通过NLST 命令获取文件。
    猜你喜欢
    • 1970-01-01
    • 2016-08-17
    • 2017-01-15
    • 1970-01-01
    • 2020-03-19
    • 2021-03-14
    • 2022-01-21
    • 2021-06-18
    • 1970-01-01
    相关资源
    最近更新 更多