首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SFTP连接的Spring测试: com.jcraft.jsch.JSchException:数据包损坏

SFTP连接的Spring测试: com.jcraft.jsch.JSchException:数据包损坏
EN

Stack Overflow用户
提问于 2017-02-20 03:50:23
回答 1查看 3.5K关注 0票数 3

我在本地:https://github.com/spring-projects/spring-integration-samples/tree/master/basic/sftp中重新创建了这个示例

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

代码语言:javascript
运行
复制
@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;
}

}

SftpCommon类:

代码语言:javascript
运行
复制
@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类:

代码语言:javascript
运行
复制
@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;
    }
}

在我的测试中,我发现了这样的东西:

代码语言:javascript
运行
复制
    @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:在org.springframework.integration.file.remote.session.CachingSessionFactory.getSession(CachingSessionFactory.java:123) at org.springframework.integration.file.remote.session.CachingSessionFactory.getSession(CachingSessionFactory.java:123) at com.spring.example.sftp.SftpOutboundTransferSample.testOutbound(SftpOutboundTransferSample.java:62) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method上创建SFTP会话失败)在java.lang.reflect.Method.invoke(Method.java:606),org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50),org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12),org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47),org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17),org.springframework.test。context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.org.junit.runners.ParentRunner$3.run(ParentRunner.java:290),org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71),org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288),org.junit.runners.ParentRunner.access$000(ParentRunner.java:58),org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268),org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks( org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) )在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)引起的: java.lang.IllegalStateException:未能在org.springframework.integration创建SFTP会话。sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:393) at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:57) at org.springframework.integration.file.remote.session.CachingSessionFactory$1.createForPool(CachingSessionFactory.java:81) at org.springframework.integration.file.remote.session.CachingSessionFactory$1.createForPool(CachingSessionFactory.java:78) at org.springframework.integration.util.SimplePool.doGetItem(SimplePool.java:188) at org.springframework.integration.util.SimplePool.org.springframework.integration.sftp.session.SftpSession.connect(SftpSession.java:273) getItem(SimplePool.java:169) . 31多个原因: java.lang.IllegalStateException:未能在org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:388)的SimplePool.java:169上连接. 36多个原因是: com.jcraft.jsch.JSchException: com.jcraft.jsch.Session.start_discard(Session.java:1067) at com.jcraft.jsch的数据包损坏的Macom.jcraft.jsch.Session.connect(Session.java:309) at com.jcraft.jsch.Session.connect(Session.java:183) at org.springframework.integration.sftp.session.SftpSession.connect(SftpSession.java:264) . 37

我在网上被搜索过,但老实说,我不知道为什么我会得到这个例外:

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

我的log4j.xml配置:

代码语言:javascript
运行
复制
<?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>

更新了的全食输出(控制台),在这里https://gist.github.com/columb1a/fe2d4dccabb6a5d9cecd3225d2a591dc

EN

回答 1

Stack Overflow用户

发布于 2017-02-21 15:35:57

解决了的问题:

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

审议的要点:

  • 在这种情况下,不幸的是,jsch的“调试”级别并不有用。
  • Stack跟踪是“提示”,因为如果您无法连接,那么这可能意味着您使用了错误的参数连接(可能)。因此,至少在我的情况下,我在会话中使用了错误的端口号。
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42336181

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档