我正在尝试SFTP from Apache Camel + JSch + SpringBoot
。要连接到Linux SFTP,它正在运行,但是对于Windows SFTP失败。在WinSCP Both Linux and Windows SFTPs are working
。
路线:
@Component
public class ReadSFTPFileRoute extends RouteBuilder {
// Scenario 1 Read file from Windows using below details not working
//default home directory /home/FTPAPPUI
//path to be used to access files /home/FTPAPPUI/share/APP_Migration
public static String user = "ABC+FTPAPPUI";
public static String pass = "A=BC#d+dEfg-2HI";
public static String host = "sftp.host.com";
public static String path = "/share/APP_Migration";
// Scenario 2 Read file from Linux using below details working
//default home directory /home/ftpappti
//path to be used to access files /home/ftpappti/tmp/app/test1
public static String user = "ftpappti";
public static String pass = "AB7cDE@F2#9Ghij";
public static String host = "1.0.0.1";
public static String path = "/tmp/app/test1";
@Override
public void configure() throws Exception {
from("sftp://"+user+"@"+host+path
+ "?strictHostKeyChecking=no"
+ "&useUserKnownHostsFile=false"
+ "&delete=true&moveFailed=.error"
+ "&password="+pass
+ "&preferredAuthentications=publickey,keyboard-interactive,password")
.process(exchange -> {
System.out.println("Processing File");
}).to("file://src/main/resources/out/"
+ "?fileName=outfile-name.txt"
+ "&fileExist=Append");
}
}
场景1的输出:( Windows)例外
main][0;39m [36mo.a.c.c.file.remote.SftpOperations [0;39m [2m:[0;39m JSCH -> Permanently added 'sftp.host.com' (RSA) to the list of known hosts.
main][0;39m [36mo.a.c.c.file.remote.SftpOperations [0;39m [2m:[0;39m JSCH -> Permanently added 'sftp.host.com' (RSA) to the list of known hosts.
main][0;39m [36mo.a.c.c.file.remote.SftpOperations [0;39m [2m:[0;39m JSCH -> Permanently added 'sftp.host.com' (RSA) to the list of known hosts.
main][0;39m [36mo.a.c.c.file.remote.SftpOperations [0;39m [2m:[0;39m JSCH -> Permanently added 'sftp.host.com' (RSA) to the list of known hosts.
main][0;39m [36mo.a.c.c.file.remote.SftpConsumer [0;39m [2m:[0;39m Error auto creating directory: share/APP_Migration due Cannot connect to sftp://ABC+FTPAPPUI@sftp.host.com:22. This exception is ignored.
org.apache.camel.component.file.GenericFileOperationFailedException: Cannot connect to sftp://ABC+FTPAPPUI@sftp.host.com:22
at org.apache.camel.component.file.remote.SftpOperations.connect(SftpOperations.java:146) ~[camel-ftp-2.19.1.jar:2.19.1]
at org.apache.camel.component.file.remote.RemoteFileConsumer.connectIfNecessary(RemoteFileConsumer.java:203) ~[camel-ftp-2.19.1.jar:2.19.1]
at org.apache.camel.component.file.remote.SftpConsumer.doStart(SftpConsumer.java:53) ~[camel-ftp-2.19.1.jar:2.19.1]
at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61) [camel-core-2.19.1.jar:2.19.1]
at org.apache.camel.impl.DefaultCamelContext.startService(DefaultCamelContext.java:3518) [camel-core-2.19.1.jar:2.19.1]
at org.apache.camel.impl.DefaultCamelContext.doStartOrResumeRouteConsumers(DefaultCamelContext.java:3835) [camel-core-2.19.1.jar:2.19.1]
at org.apache.camel.impl.DefaultCamelContext.doStartRouteConsumers(DefaultCamelContext.java:3771) [camel-core-2.19.1.jar:2.19.1]
at org.apache.camel.impl.DefaultCamelContext.safelyStartRouteServices(DefaultCamelContext.java:3691) [camel-core-2.19.1.jar:2.19.1]
at org.apache.camel.impl.DefaultCamelContext.doStartOrResumeRoutes(DefaultCamelContext.java:3455) [camel-core-2.19.1.jar:2.19.1]
at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:3309) [camel-core-2.19.1.jar:2.19.1]
at org.apache.camel.impl.DefaultCamelContext.access$000(DefaultCamelContext.java:202) [camel-core-2.19.1.jar:2.19.1]
at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3093) [camel-core-2.19.1.jar:2.19.1]
at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3089) [camel-core-2.19.1.jar:2.19.1]
at org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:3112) [camel-core-2.19.1.jar:2.19.1]
at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:3089) [camel-core-2.19.1.jar:2.19.1]
at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61) [camel-core-2.19.1.jar:2.19.1]
at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:3026) [camel-core-2.19.1.jar:2.19.1]
at org.apache.camel.spring.boot.RoutesCollector.maybeStart(RoutesCollector.java:203) [camel-spring-boot-2.19.1.jar:2.19.1]
at org.apache.camel.spring.boot.RoutesCollector.onApplicationEvent(RoutesCollector.java:178) [camel-spring-boot-2.19.1.jar:2.19.1]
at org.apache.camel.spring.boot.RoutesCollector.onApplicationEvent(RoutesCollector.java:50) [camel-spring-boot-2.19.1.jar:2.19.1]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) [spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) [spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) [spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:393) [spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:347) [spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:883) [spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) [spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at com.learnjovi.route.Application.main(Application.java:10) [classes/:na]
Caused by: com.jcraft.jsch.JSchException: SSH_MSG_DISCONNECT: 2 Too many authentication failures
at com.jcraft.jsch.Session.read(Session.java:1004) ~[jsch-0.1.54.jar:na]
at com.jcraft.jsch.UserAuthPassword.start(UserAuthPassword.java:91) ~[jsch-0.1.54.jar:na]
at com.jcraft.jsch.Session.connect(Session.java:470) ~[jsch-0.1.54.jar:na]
at org.apache.camel.component.file.remote.SftpOperations.connect(SftpOperations.java:118) ~[camel-ftp-2.19.1.jar:2.19.1]
... 34 common frames omitted
main][0;39m [36mo.a.camel.spring.SpringCamelContext [0;39m [2m:[0;39m Route: route4 started and consuming from: sftp://ABC+FTPAPPUI@sftp.host.com/share/APP_Migration?delete=true&moveFailed=.error&password=xxxxxx&preferredAuthentications=publickey%2Ckeyboard-interactive%2Cpassword&strictHostKeyChecking=no&useUserKnownHostsFile=false
main][0;39m [36mo.a.camel.spring.SpringCamelContext [0;39m [2m:[0;39m Total 1 routes, of which 1 are started.
场景2的输出:(Linux)
main] o.a.c.c.file.remote.SftpOperations : JSCH -> Permanently added '1.0.0.1' (RSA) to the list of known hosts.
main] o.a.camel.spring.SpringCamelContext : Route: route4 started and consuming from: sftp://ftpappti@1.0.0.1/tmp/app/test1?delete=true&moveFailed=.error&password=xxxxxx&preferredAuthentications=publickey%2Ckeyboard-interactive%2Cpassword&strictHostKeyChecking=no&useUserKnownHostsFile=false
main] o.a.camel.spring.SpringCamelContext : Total 1 routes, of which 1 are started.
main] o.a.camel.spring.SpringCamelContext : Apache Camel 2.19.1 (CamelContext: camel-1) started in 6.805 seconds
main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
main] com.learnjovi.route.Application : Started Application in 13.878 seconds (JVM running for 16.309)
Processing File
的pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>2.21.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-stream</artifactId>
<version>2.21.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-ftp</artifactId>
<version>2.21.0</version>
</dependency>
</dependencies>
答案 0 :(得分:1)
在URI的查询字符串中使用特殊字符时,需要使用RAW
括起值,否则它将成为URL编码。
这似乎并不适用于查询字符串之前的任何内容(但根据我的经验)。因此,请使用RAW
附上您的密码值,它应该可以正常工作。
from("sftp://"+user+"@"+host+path + "?strictHostKeyChecking=no" + "&useUserKnownHostsFile=false" + "&delete=true&moveFailed=.error" + "&password=RAW("+pass+")&preferredAuthentications=publickey,keyboard-interactive,password")