骆驼无法使用streamDownload = true删除sftp

时间:2019-09-16 07:08:27

标签: apache-camel sftp

我有一个小的骆驼路线和文件过滤器,用于查找比配置的天数长的文件。如果文件过滤器将文件接受到路由中,则路由仅记录一条消息并结束,从而允许用户在交换完成处理后删除文件( delete = true ):

String uri="sftp://user@sftpsite:22/in/archive?delay=5m&include=.*\.zip&eagerMaxMessagesPerPoll=false&maxMessagesPerPoll=1000&streamDownload=true&stepwise=false&disconnect=true&connectTimeout=60000&filter=#purgeFileFilter&delete=true&password=pazz&sortBy=file:modified";

from(uri).id("PurgeFile").routeId("PurgeFile")
    .setHeader("errorMsg", simple("${date:now} Error purging file ${file:name}")
    .log("File purged: ${file:name}")
;  

查看code时,我看到通过设置 streamDownload = true 可以不读取文件。这很好,因为我还是删除文件而不使用内容

 private boolean retrieveFileToStreamInBody(String name, Exchange exchange) throws GenericFileOperationFailedException {
    String currentDir = null;
    try {
        GenericFile<ChannelSftp.LsEntry> target = (GenericFile<ChannelSftp.LsEntry>)exchange.getProperty(FileComponent.FILE_EXCHANGE_FILE);
        ObjectHelper.notNull(target, "Exchange should have the " + FileComponent.FILE_EXCHANGE_FILE + " set");

        String remoteName = name;
        if (endpoint.getConfiguration().isStepwise()) {
            // remember current directory
            currentDir = getCurrentDirectory();

            // change directory to path where the file is to be retrieved
            // (must do this as some FTP servers cannot retrieve using
            // absolute path)
            String path = FileUtil.onlyPath(name);
            if (path != null) {
                changeCurrentDirectory(path);
            }
            // remote name is now only the file name as we just changed
            // directory
            remoteName = FileUtil.stripPath(name);
        }

        // use input stream which works with Apache SSHD used for testing
        InputStream is = channel.get(remoteName);

        if (endpoint.getConfiguration().isStreamDownload()) {
            target.setBody(is);
            exchange.getIn().setHeader(RemoteFileComponent.REMOTE_FILE_INPUT_STREAM, is);
        } else {
            // read the entire file into memory in the byte array
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            IOHelper.copyAndCloseInput(is, bos);
            // close the stream after done
            IOHelper.close(bos);

            target.setBody(bos.toByteArray());
        }

没有 streamDownload = true 的情况下,我可以删除sftp站点上的一个文件,但是似乎首先读取了该文件中的所有数据,因此为什么我想将streamDownload设置为true 。但是,当我将其设置为true时,不会删除该文件,并且会出现以下错误:

org.apache.camel.component.file.GenericFileOperationFailedException: Cannot delete file: in/archive/foo.zip
        at org.apache.camel.component.file.remote.SftpOperations.deleteFile(SftpOperations.java:452)
        at org.apache.camel.component.file.strategy.GenericFileDeleteProcessStrategy.commit(GenericFileDeleteProcessStrategy.java:72)
        at org.apache.camel.component.file.GenericFileOnCompletion.processStrategyCommit(GenericFileOnCompletion.java:127)
        at org.apache.camel.component.file.GenericFileOnCompletion.onCompletion(GenericFileOnCompletion.java:83)
        at org.apache.camel.component.file.GenericFileOnCompletion.onComplete(GenericFileOnCompletion.java:59)
        at org.apache.camel.util.UnitOfWorkHelper.doneSynchronizations(UnitOfWorkHelper.java:104)
        at org.apache.camel.impl.DefaultUnitOfWork.done(DefaultUnitOfWork.java:243)
        at org.apache.camel.util.UnitOfWorkHelper.doneUow(UnitOfWorkHelper.java:65)
        at org.apache.camel.processor.CamelInternalProcessor$UnitOfWorkProcessorAdvice.after(CamelInternalProcessor.java:685)
        at org.apache.camel.processor.CamelInternalProcessor$UnitOfWorkProcessorAdvice.after(CamelInternalProcessor.java:634)
        at org.apache.camel.processor.CamelInternalProcessor$InternalCallback.done(CamelInternalProcessor.java:251)
        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:183)
        at org.apache.camel.component.file.GenericFileConsumer.processExchange(GenericFileConsumer.java:454)
        at org.apache.camel.component.file.remote.RemoteFileConsumer.processExchange(RemoteFileConsumer.java:138)
        at org.apache.camel.component.file.GenericFileConsumer.processBatch(GenericFileConsumer.java:223)
        at org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:187)
        at org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:174)
        at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:101)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: com.jcraft.jsch.SftpException: Permission denied
        at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2873)
        at com.jcraft.jsch.ChannelSftp.rm(ChannelSftp.java:1985)
        at org.apache.camel.component.file.remote.SftpOperations.deleteFile(SftpOperations.java:448) 

0 个答案:

没有答案