3

我一直在我的本地重新创建这个例子:https ://github.com/spring-projects/spring-integration-samples/tree/master/basic/sftp

但这次只使用注释而不是 xml 配置。我有这样的事情:

@Configuration
public class SftpExampleBeanClasses extends SftpConfig {

@Bean
public CachingSessionFactory<LsEntry> sftpSessionFactory() throws IOException {
    DefaultSftpSessionFactory sftpSessionFactory = new DefaultSftpSessionFactory();
    sftpSessionFactory.setHost(this.host);
    sftpSessionFactory.setPort(this.port);
    sftpSessionFactory.setUser(this.username);
    InputStream stream = new ClassPathResource(this.privatekey).getInputStream();
    sftpSessionFactory.setPrivateKey(new ByteArrayResource(StreamUtils.copyToByteArray(stream)));
    sftpSessionFactory.setPrivateKeyPassphrase(this.passphrase);
    sftpSessionFactory.setAllowUnknownKeys(true);
    return new CachingSessionFactory<LsEntry>(sftpSessionFactory);

    //return sftpSessionFactory;
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Bean
@ServiceActivator(inputChannel = "ftpChannel", adviceChain = "retryAdvice")
public MessageHandler ftpHandler() throws IOException {
    FileTransferringMessageHandler handler = new FileTransferringMessageHandler(this.sftpSessionFactory());
    handler.setRemoteDirectoryExpression(new LiteralExpression("/"));
    return handler;
}

}

Sftp普通类:

@Configuration
public class SftpCommon extends SftpConfig {

    @Bean
    public int serverPort(){
        if(this.port == -1){
            return EmbeddedSftpServer.PORT;
        }
        return this.port;
    }

    @Bean
    public EmbeddedSftpServer embeddedSftpServer(){
         EmbeddedSftpServer embeddedSftpServer = new EmbeddedSftpServer();
         embeddedSftpServer.setPort(serverPort());
         return embeddedSftpServer;
    }   
}

SftpConfig 类:

@Configuration
@PropertySource(value="classpath:user.properties")
public class SftpConfig {

    @Value("${port}")
    protected
    int port;

    @Value("${username}")
    protected
    String username;

    @Value("${passphrase}")
    protected
    String passphrase;

    @Value("${host}")
    protected
    String host;

    @Value("${private.keyfile}")
    protected
    String  privatekey;

    public String getHost() {
        return host;
    }

    @Bean
    public String getUsername() {
        return this.username;
    }

    @Bean
    public String getPassphrase() {
        return this.passphrase;
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public String getPrivatekey() {
        return privatekey;
    }
}

在我的测试中,我有这样的事情:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes ={SftpExampleBeanClasses.class,SftpCommon.class, SftpConfig.class})
    public class SftpOutboundTransferSample {

    @Autowired
    ApplicationContext applicationContext;

    @Test
    public void testOutbound() throws Exception{

        final String sourceFileName = "README.md";
        final String destinationFileName = sourceFileName +"_foo";

        @SuppressWarnings("unchecked")
        SessionFactory<LsEntry> sessionFactory = (SessionFactory<LsEntry>)


        applicationContext.getBean(CachingSessionFactory.class);
        /* this is the line where I'm getting issues */
        sessionFactory.getSession();
    }

我的 stackTrace 是这个:

org.springframework.messaging.MessagingException: 获取池项失败;嵌套异常是 java.lang.IllegalStateException: failed to create SFTP Session at org.springframework.integration.util.SimplePool.getItem(SimplePool.java:178) at org.springframework.integration.file.remote.session.CachingSessionFactory.getSession( CachingSessionFactory.java:123) 在 com.spring.example.sftp.SftpOutboundTransferSample.testOutbound(SftpOutboundTransferSample.java:62) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java: 57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:606) 在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.爪哇:

我已经在 Internet 上进行了搜索,但老实说,我不明白为什么会出现此异常:

com.jcraft.jsch.JSchException:数据包损坏

我的 log4j.xml 配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <!-- Appenders -->
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{HH:mm:ss.SSS} %-5p [%t][%c] %m%n" />
        </layout>
    </appender>

    <!-- Loggers -->
    <logger name="org.springframework.integration">
        <level value="warn" />
    </logger>

    <logger name="org.springframework.integration.samples">
        <level value="info" />
    </logger>

    <logger name="log4j.category.com.jcraft.jsch">
        <level value="debug" />
        <appender-ref ref="console" />
    </logger>

    <!-- Root Logger -->
    <root>
        <priority value="debug" />
        <appender-ref ref="console" />
    </root>

</log4j:configuration>

[更新]完整 ECLIPSE 的输出(控制台)在这里https ://gist.github.com/columb1a/fe2d4dccabb6a5d9cecd3225d2a591dc

4

1 回答 1

2

解决了我的问题:

在我的 *.properties 文件中,服务器的端口号是 -1。然后服务器尝试启动,如果它能够找到-1以外的端口(随机端口),那么它会自动启动。然后我必须在我的 SFTP 会话中注入那个随机端口号。关键是,我在我的 sftp 会话中使用了“-1”值(在我的 *.properties 文件中声明),而不是使用服务器生成的随机值。然后我的会话无法连接到服务器。

需要考虑的要点:

  • 不幸的是,在这种情况下,“调试”级别jsch没有用。
  • 堆栈跟踪是“提示”,因为不知何故,如果您无法连接,那么这可能意味着您使用了错误的参数进行连接(也许)。因此,至少在我的情况下,我在会话中使用了错误的端口号。
于 2017-02-21T15:35:57.543 回答