【问题标题】:Not able delete remote file located on a server using apache camel FTP无法使用 apache camel FTP 删除位于服务器上的远程文件
【发布时间】:2014-01-29 13:39:02
【问题描述】:

我正在处理一段代码,我需要处理位于使用 Apache camel 的服务器上的文件。我需要实现以下步骤:

1 --> 控件应该遍历位于特定文件夹中的所有文件。

2 --> 然后它应该检查哪个文件超过 15 天。

3 --> 然后删除超过 15 天的文件。

现在,我可以实现第一步和第二步了。但我无法删除该文件。 我创建了这样的骆驼路线:

<camel:route id="lastModifiedFMFileCheckRoute">
            <camel:from uri="sftp://someUser@someServer/usableFiles?password=secret"/>
            <camel:setProperty propertyName="availableFile">
                <camel:simple>${body}</camel:simple>
            </camel:setProperty>
            <camel:process ref="fileModificationDateProcessor" />
</camel:route>  

我的处理器的处理方法看起来像这样,我正在检查文件是否超过 15 天。

  @Override
        public void process(Exchange exchange) throws Exception {
            boolean isFileDeleted = false;
            @SuppressWarnings("rawtypes")
            GenericFile currentFile = (GenericFile)exchange.getProperty("availableFile", RemoteFile.class); 
            Date currentDate = new Date();
            int numberOfDays = (int)( (currentDate.getTime() - availableFile.getLastModified()) / MILLISECONDS_TO_DAY_CONVERTER_VALUE);
            if(numberOfDays > 15){
                String absoluteFilePath = availableFile.getAbsoluteFilePath();
                //TODO  The file (currentFile) needs to be deleted. As it is older than 15 days.

        }
        exchange.getOut().setBody(fileDeleted);

    }

这里如何实现所需文件的删除。

【问题讨论】:

    标签: java ftp apache-camel sftp


    【解决方案1】:

    在您的 (s)ftp 端点上,您可以设置 delete=truefilter=#ageFilter 其中 ageFilter 是对仅接受超过 15 天的文件的自定义文件过滤器的引用。

    public class AgeFilter<T> implements GenericFileFilter<T> {
    
        @Override
        public boolean accept(final GenericFile<T> file) {
            long now = System.currentTimeMillis();
            long lastModified = file.getLastModified();
    
            return now-lastModified > CUT_OFF_AGE;
        }
    }
    

    在路由结束时,(s)ftp 端点将删除此过滤器选择的所有文件。

    作为文件过滤器的替代方案,如果文件超过 15 天,您可以在处理器中添加 RuntimeException。这将使您的路由失败,并将文件留在远程服务器上。但是,对控制流使用异常确实不是一个好习惯。

    【讨论】:

      猜你喜欢
      • 2014-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多