如何使用camel将文件从源复制到目标

时间:2017-08-24 11:06:34

标签: java apache apache-camel scp

我们需要将文件从源复制到目标(与源不同的系统)。我们需要使用apache camel。

我没有找到如何指定目标目录。你能帮忙吗

private void boot() throws Exception {
        Main main = new Main();
        main.addRouteBuilder(new RouteBuilder() {
            String sourceDirectory = "D:\\vsinv-conf";
            String server = "xxx-yyy-host:/var/log/index/";
            String user = "admin";
            String password = "password";
            public void configure() {
                from("timer:loadingApplicationList?repeatCount=1").routeId("loadApplicationList").to("direct:start");
                from("direct:start").from("file://" + sourceDirectory).to("scp://" + server + "?username=" + user
                        + "&password=" + password + "&useUserKnownHostsFile=false");
            }
        });

        main.run();
    }

发出以下异常

java.lang.NullPointerException
    at com.jcraft.jsch.Util.checkTilde(Util.java:489)
    at com.jcraft.jsch.KnownHosts.setKnownHosts(KnownHosts.java:53)
    at com.jcraft.jsch.JSch.setKnownHosts(JSch.java:317)
    at org.apache.camel.component.scp.ScpOperations.createSession(ScpOperations.java:239)
    at org.apache.camel.component.scp.ScpOperations.connect(ScpOperations.java:179)
    at org.apache.camel.component.file.remote.RemoteFileProducer.connectIfNecessary(RemoteFileProducer.java:209)
    at org.apache.camel.component.file.remote.RemoteFileProducer.recoverableConnectIfNecessary(RemoteFileProducer.java:201)
    at org.apache.camel.component.file.remote.RemoteFileProducer.preWriteCheck(RemoteFileProducer.java:133)
    at org.apache.camel.component.file.GenericFileProducer.processExchange(GenericFileProducer.java:113)
    at org.apache.camel.component.file.remote.RemoteFileProducer.process(RemoteFileProducer.java:58)
    at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
    at org.apache.camel.processor.SendProcessor$2.doInAsyncProducer(SendProcessor.java:173)
    at org.apache.camel.impl.ProducerCache.doInAsyncProducer(ProducerCache.java:341)
    at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:168)
    at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:468)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:196)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:196)
    at org.apache.camel.component.file.GenericFileConsumer.processExchange(GenericFileConsumer.java:454)
    at org.apache.camel.component.file.GenericFileConsumer.processBatch(GenericFileConsumer.java:226)
    at org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:190)
    at org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:175)
    at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:102)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
2017-08-24 16:29:05 WARN  ScpProducer:86 - Writing file failed with: null

1 个答案:

答案 0 :(得分:1)

我知道你正试图从文件复制到SCP。

因此,从Jsch component开始,我相信您有两种方法可以实现这一目标:

  

您可以使用以下格式将查询选项附加到URI,   ?option = value& option = value& ...也可以指定文件名   在URI的一部分或作为“CamelFileName”标题   消息(如果在代码中使用,则为Exchange.FILE_NAME。)

这意味着您可以使用:

to("scp://yourhost/folder1/folder2/youfile.txt?options..")

或者您可以尝试使用Exchange.FILE_NAME(虽然我不知道它是否允许您在那里设置目录或文件名:

from("direct:start")
.setHeader(Exchange.FILE_NAME, constant("folder1/folder2/youfile.txt"))
.to("scp://host?options...");