1

我需要通过 sftp 连接到 ftp 服务器。我通过 ftp 和 ftps 成功连接了骆驼。

public static RouteBuilder getRouteBuilderSFTPS() {
        return new RouteBuilder() {
            public void configure() {
                from("sftp://666.66.666.66:22/POLP-FROAKS?fileName=testMsg.txt&username=POLP-FROAKS&password=pass&strictHostKeyChecking=no&ciphers=blowfish-cbc,aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,aes192-cbc,aes256-cbc").
                to("file://target/test-reports667");
            }
        };
    }

我得到响应 org.apache.camel.component.file.GenericFileOperationFailedException: Cannot change directory to: POLP-FROAKS

完整日志:

16:25:32,758  INFO (main) [DefaultCamelContext] Apache Camel 2.9.2 (CamelContext: camel-1) started in 0.472 seconds
Apache Camel 2.9.2 (CamelContext: camel-1) started in 0.472 seconds
16:25:33,749 DEBUG (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpConsumer] Not connected/logged in, connecting to: sftp://POLP-FROAKS@216.66.216.10:22
Not connected/logged in, connecting to: sftp://POLP-FROAKS@216.66.216.10:22
16:25:33,753 DEBUG (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] Using ciphers: blowfish-cbc,aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,aes192-cbc,aes256-cbc
Using ciphers: blowfish-cbc,aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,aes192-cbc,aes256-cbc
16:25:33,764 DEBUG (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] Using StrickHostKeyChecking: no
Using StrickHostKeyChecking: no
16:25:33,769  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> Connecting to 216.66.216.10 port 22
JSCH -> Connecting to 216.66.216.10 port 22
16:25:34,099  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> Connection established
JSCH -> Connection established
16:25:34,476  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> Remote version string: SSH-2.0-OpenSSH_4.3
JSCH -> Remote version string: SSH-2.0-OpenSSH_4.3
16:25:34,476  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> Local version string: SSH-2.0-JSCH-0.1.48
JSCH -> Local version string: SSH-2.0-JSCH-0.1.48
16:25:34,476  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
JSCH -> CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
16:25:34,760  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> aes256-ctr is not available.
JSCH -> aes256-ctr is not available.
16:25:34,760  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> aes192-ctr is not available.
JSCH -> aes192-ctr is not available.
16:25:34,760  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> aes256-cbc is not available.
JSCH -> aes256-cbc is not available.
16:25:34,760  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> aes192-cbc is not available.
JSCH -> aes192-cbc is not available.
16:25:34,760  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> arcfour256 is not available.
JSCH -> arcfour256 is not available.
16:25:34,761  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> CheckKexes: diffie-hellman-group14-sha1
JSCH -> CheckKexes: diffie-hellman-group14-sha1
16:25:34,768  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> diffie-hellman-group14-sha1 is not available.
JSCH -> diffie-hellman-group14-sha1 is not available.
16:25:34,804  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> SSH_MSG_KEXINIT sent
JSCH -> SSH_MSG_KEXINIT sent
16:25:34,988  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> SSH_MSG_KEXINIT received
JSCH -> SSH_MSG_KEXINIT received
16:25:34,988  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: server: diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
JSCH -> kex: server: diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
16:25:34,988  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: server: ssh-rsa,ssh-dss,ssh-dss,ssh-rsa
JSCH -> kex: server: ssh-rsa,ssh-dss,ssh-dss,ssh-rsa
16:25:34,988  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: server: aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour128,arcfour256,arcfour,aes192-cbc,aes256-cbc,aes128-ctr,aes192-ctr,aes256-ctr
JSCH -> kex: server: aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour128,arcfour256,arcfour,aes192-cbc,aes256-cbc,aes128-ctr,aes192-ctr,aes256-ctr
16:25:34,988  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: server: aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour128,arcfour256,arcfour,aes192-cbc,aes256-cbc,aes128-ctr,aes192-ctr,aes256-ctr
JSCH -> kex: server: aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour128,arcfour256,arcfour,aes192-cbc,aes256-cbc,aes128-ctr,aes192-ctr,aes256-ctr
16:25:34,988  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: server: hmac-md5,hmac-sha1,hmac-ripemd160,hmac-sha1-96,hmac-md5-96
JSCH -> kex: server: hmac-md5,hmac-sha1,hmac-ripemd160,hmac-sha1-96,hmac-md5-96
16:25:34,988  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: server: hmac-md5,hmac-sha1,hmac-ripemd160,hmac-sha1-96,hmac-md5-96
JSCH -> kex: server: hmac-md5,hmac-sha1,hmac-ripemd160,hmac-sha1-96,hmac-md5-96
16:25:34,988  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: server: none,zlib@openssh.com
JSCH -> kex: server: none,zlib@openssh.com
16:25:34,988  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: server: none,zlib@openssh.com
JSCH -> kex: server: none,zlib@openssh.com
16:25:34,988  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: server: 
JSCH -> kex: server: 
16:25:34,988  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: server: 
JSCH -> kex: server: 
16:25:34,988  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: client: diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1
JSCH -> kex: client: diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1
16:25:34,988  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: client: ssh-rsa,ssh-dss
JSCH -> kex: client: ssh-rsa,ssh-dss
16:25:34,989  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: client: blowfish-cbc,aes128-ctr,aes128-cbc,3des-ctr,3des-cbc
JSCH -> kex: client: blowfish-cbc,aes128-ctr,aes128-cbc,3des-ctr,3des-cbc
16:25:34,989  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: client: blowfish-cbc,aes128-ctr,aes128-cbc,3des-ctr,3des-cbc
JSCH -> kex: client: blowfish-cbc,aes128-ctr,aes128-cbc,3des-ctr,3des-cbc
16:25:34,989  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: client: hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96
JSCH -> kex: client: hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96
16:25:34,989  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: client: hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96
JSCH -> kex: client: hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96
16:25:34,989  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: client: none
JSCH -> kex: client: none
16:25:34,989  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: client: none
JSCH -> kex: client: none
16:25:34,989  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: client: 
JSCH -> kex: client: 
16:25:34,989  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: client: 
JSCH -> kex: client: 
16:25:34,989  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: server->client blowfish-cbc hmac-md5 none
JSCH -> kex: server->client blowfish-cbc hmac-md5 none
16:25:34,989  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> kex: client->server blowfish-cbc hmac-md5 none
JSCH -> kex: client->server blowfish-cbc hmac-md5 none
16:25:35,015  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> SSH_MSG_KEXDH_INIT sent
JSCH -> SSH_MSG_KEXDH_INIT sent
16:25:35,015  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> expecting SSH_MSG_KEXDH_REPLY
JSCH -> expecting SSH_MSG_KEXDH_REPLY
16:25:35,451  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> ssh_rsa_verify: signature true
JSCH -> ssh_rsa_verify: signature true
16:25:35,455  WARN (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> Permanently added '216.66.216.10' (RSA) to the list of known hosts.
JSCH -> Permanently added '216.66.216.10' (RSA) to the list of known hosts.
16:25:35,455  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> SSH_MSG_NEWKEYS sent
JSCH -> SSH_MSG_NEWKEYS sent
16:25:35,455  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> SSH_MSG_NEWKEYS received
JSCH -> SSH_MSG_NEWKEYS received
16:25:35,466  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> SSH_MSG_SERVICE_REQUEST sent
JSCH -> SSH_MSG_SERVICE_REQUEST sent
16:25:35,779  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> SSH_MSG_SERVICE_ACCEPT received
JSCH -> SSH_MSG_SERVICE_ACCEPT received
16:25:36,093  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> Authentications that can continue: publickey,keyboard-interactive,password
JSCH -> Authentications that can continue: publickey,keyboard-interactive,password
16:25:36,094  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> Next authentication method: publickey
JSCH -> Next authentication method: publickey
16:25:36,095  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> Authentications that can continue: password
JSCH -> Authentications that can continue: password
16:25:36,095  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> Next authentication method: password
JSCH -> Next authentication method: password
16:25:36,835  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> Authentication succeeded (password).
JSCH -> Authentication succeeded (password).
16:25:37,932  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] Connected to sftp://POLP-FROAKS@216.66.216.10:22
Connected to sftp://POLP-FROAKS@216.66.216.10:22
16:25:37,932  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpConsumer] Connected and logged in to: sftp://POLP-FROAKS@216.66.216.10:22
Connected and logged in to: sftp://POLP-FROAKS@216.66.216.10:22
16:25:38,966  WARN (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [RemoteFilePollingConsumerPollStrategy] Trying to recover by disconnecting from remote server forcing a re-connect at next poll: sftp://POLP-FROAKS@216.66.216.10:22
Trying to recover by disconnecting from remote server forcing a re-connect at next poll: sftp://POLP-FROAKS@216.66.216.10:22
16:25:38,966 DEBUG (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpConsumer] Disconnecting from: sftp://POLP-FROAKS@216.66.216.10:22
Disconnecting from: sftp://POLP-FROAKS@216.66.216.10:22
16:25:38,966  INFO (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [SftpOperations] JSCH -> Disconnecting from 216.66.216.10 port 22
JSCH -> Disconnecting from 216.66.216.10 port 22
16:25:38,971  INFO (Connect thread 216.66.216.10 session) [SftpOperations] JSCH -> Caught an exception, leaving main loop due to socket closed
JSCH -> Caught an exception, leaving main loop due to socket closed
16:25:38,977  WARN (Camel (camel-1) thread #1 - sftp://216.66.216.10:22/POLP-FROAKS) [RemoteFilePollingConsumerPollStrategy] Consumer Consumer[sftp://216.66.216.10:22/POLP-FROAKS?ciphers=blowfish-cbc,aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,aes192-cbc,aes256-cbc&fileName=testMsg.txt&password=******&strictHostKeyChecking=no&username=POLP-FROAKS] could not poll endpoint: Endpoint[sftp://216.66.216.10:22/POLP-FROAKS?ciphers=blowfish-cbc,aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,aes192-cbc,aes256-cbc&fileName=testMsg.txt&password=******&strictHostKeyChecking=no&username=POLP-FROAKS] caused by: Cannot change directory to: POLP-FROAKS
org.apache.camel.component.file.GenericFileOperationFailedException: Cannot change directory to: POLP-FROAKS
    at org.apache.camel.component.file.remote.SftpOperations.doChangeDirectory(SftpOperations.java:423)
    at org.apache.camel.component.file.remote.SftpOperations.changeCurrentDirectory(SftpOperations.java:410)
    at org.apache.camel.component.file.remote.SftpConsumer.doPollDirectory(SftpConsumer.java:78)
    at org.apache.camel.component.file.remote.SftpConsumer.pollDirectory(SftpConsumer.java:50)
    at org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:100)
    at org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:138)
    at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:90)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:317)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:150)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(ScheduledThreadPoolExecutor.java:98)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:204)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: 2: No such file
    at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2569)
    at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:2018)
    at com.jcraft.jsch.ChannelSftp.cd(ChannelSftp.java:316)
    at org.apache.camel.component.file.remote.SftpOperations.doChangeDirectory(SftpOperations.java:421)
    ... 15 more

我有相同的代码,但没有骆驼,它可以工作。骆驼版有什么问题?我也有点困惑为什么服务器不问我关于私钥的问题?

更新:

我正在尝试添加私钥:

JSch.setLogger(new MyJschLogger());
            JSch ssh = new JSch();

            final byte[] privateKey = getPrivateKeyAsByteStream(privateKeyPath);
            final byte[] passPrivateKey = "".getBytes();
            //TODO DB: invalid private key. If not use private key than we can download file
            ssh.addIdentity(username, privateKey, null, passPrivateKey);

            session = ssh.getSession(username, server, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");

            session.connect();
            channel = session.openChannel("sftp");
            channel.connect();
            ChannelSftp sftp = (ChannelSftp) channel;

            final Vector files = sftp.ls(".");
            Iterator itFiles = files.iterator();
            while (itFiles.hasNext()) {
                System.out.println("Index: " + itFiles.next());
            }

            sftp.get(remoteFile, localFile);
            sftp.exit();

已读取密钥,但出现错误:

com.jcraft.jsch.JSchException: invalid privatekey: POLP-FROAKS
    at com.jcraft.jsch.IdentityFile.<init>(IdentityFile.java:302)
    at com.jcraft.jsch.IdentityFile.newInstance(IdentityFile.java:135)
    at com.jcraft.jsch.JSch.addIdentity(JSch.java:390)
    at com.nxsystems.camel.Main.retrieveFileViaSFTP(Main.java:204)
    at com.nxsystems.camel.Main.main(Main.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

密钥是用 PuTTY 生成的。FilleZilla 正常接受密钥。为什么????

更新:noup。问题不在钥匙上。我使用 puttyge.exe->conversion 将 putty 格式的密钥转换为 openssh 格式,JSCH 成功接受了它。但是旧错误“无法将目录更改为...”又回来了...

4

2 回答 2

1

最近在更改目录时遇到了同样的问题。解决方案是从文档中设置 stepwise=false :

URI 选项:逐步,默认 = true:Camel 2.6:是否应使用逐步遍历目录。逐步意味着它将一次 CD 一个目录。请参阅下面的更多详细信息。如果您不能使用这种方法,您可以禁用它

于 2013-11-11T11:18:51.077 回答
0

您将 POLP-FROAKS 指定为 FTP 服务器的起始目录。所以当 Camel 登录到 FTP 服务器时,它会在登录后做一个 CD POLP-FROAKS。如果您不应该更改为子目录,则将其从端点 uri 中删除。例如,它只是

from("sftp://666.66.666.66:22/?xxx...
于 2012-05-11T06:30:01.553 回答