当前位置: 首页 > 知识库问答 >
问题:

spring集成-SFTP确保上传成功

裴威
2023-03-14

如何确保文件是否成功上传到SFTP服务器。我想确保SFTP上传成功,然后我只想应用其他逻辑。这是我要上传的代码。

 @Bean
 public DefaultSftpSessionFactory sftpSessionFactory() throws IOException {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
    factory.setHost(config.getSftpHost());
    factory.setPort(Integer.parseInt(config.getSftpPort()));
    factory.setUser(config.getSftpUser());
    factory.setAllowUnknownKeys(true);
    factory.setTimeout(10000);
    factory.setPrivateKey(new ClassPathResource(config.getPrivateKey()));
   } 
@Bean
   @ServiceActivator(inputChannel = "toSftpChannel")
   public SftpMessageHandler uploadHandler() throws IOException {
    SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
    handler.setRemoteDirectoryExpression(new LiteralExpression(config.getSftpRemoteDirectory()));
    handler.setFileNameGenerator(new FileNameGenerator() {
    @Override
    public String generateFileName(Message<?> message) {
       if (message.getPayload() instanceof File) {
           return ((File) message.getPayload()).getName();
        } else {
            throw new IllegalArgumentException("File expected as payload.");
        }
      }
    });
   return handler;
  }
 @MessagingGateway
   public interface UploadGateway {
   @Gateway(requestChannel = "toSftpChannel")
    void upload(File file);
   }

我看到日志看起来不错。已验证文件正在上传到SFTP服务器。但不知何故,我无法确保文件正在上传到服务器上。请告知如何添加代码以确保文件上传成功。

  jsch Connecting to info.xyz.com port 22
  jsch Connection established
  jsch Remote version string: SSH-.0-Internal_SFTP__100
  jsch Local version string: SSH-.0-JSCH-0.1.4
  jsch CheckCiphers: aes6-ctr,aes19-ctr,aes18-ctr,aes6-cbc,aes19-cbc,aes18-cbc,des-ctr,arcfour,arcfour18,arcfour6
  jsch CheckKexes: diffie-hellman-group14-sha1,ecdh-sha-nistp6,ecdh-sha-nistp84,ecdh-sha-nistp1
  jsch CheckSignatures: ecdsa-sha-nistp6,ecdsa-sha-nistp84,ecdsa-sha-nistp1
  jsch SSH_MSG_KEXINIT sent
  jsch SSH_MSG_KEXINIT received
  jsch kex: server: diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha6,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1
  jsch kex: server: ssh-rsa
  jsch kex: server: aes18-ctr,aes6-cbc,aes19-cbc,aes18-cbc,aes6-ctr,aes19-ctr,des-cbc,blowfish-cbc,arcfour,arcfour18,arcfour6
  jsch kex: server: aes18-ctr,aes6-cbc,aes19-cbc,aes18-cbc,aes6-ctr,aes19-ctr,des-cbc,blowfish-cbc,arcfour,arcfour18,arcfour6
  jsch kex: server: hmac-sha-6,hmac-sha1-96,hmac-sha1,hmac-sha-1,hmac-md,hmac-sha-1-96,hmac-sha-6-96,hmac-md-96,hmac-sha6,hmac-sha6@ssh.com
  jsch kex: server: hmac-sha6,hmac-sha-6,hmac-sha1-96,hmac-sha1,hmac-sha-1,hmac-md,hmac-sha-1-96,hmac-sha-6-96,hmac-md-96,hmac-sha6@ssh.com
  jsch kex: server: none
  jsch kex: server: none
  jsch kex: server: 
  jsch kex: server: 
  jsch kex: client: ecdh-sha-nistp6,ecdh-sha-nistp84,ecdh-sha-nistp1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha6,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
  jsch kex: client: ssh-rsa,ssh-dss,ecdsa-sha-nistp6,ecdsa-sha-nistp84,ecdsa-sha-nistp1
  jsch kex: client: aes18-ctr,aes18-cbc,des-ctr,des-cbc,blowfish-cbc,aes19- 
 ctr,aes19-cbc,aes6-ctr,aes6-cbc
  jsch kex: client: aes18-ctr,aes18-cbc,des-ctr,des-cbc,blowfish-cbc,aes19- 
   ctr,aes19-cbc,aes6-ctr,aes6-cbc
  jsch kex: client: hmac-md,hmac-sha1,hmac-sha-6,hmac-sha1-96,hmac-md-96
  jsch kex: client: hmac-md,hmac-sha1,hmac-sha-6,hmac-sha1-96,hmac-md-96
  jsch kex: client: none
  jsch kex: client: none
  jsch kex: client: 
  jsch kex: client: 
  jsch kex: server->client aes1-ctr hmac-md none
  jsch kex: client->server aes1-ctr hmac-md none
  jsch SSH_MSG_KEXDH_INIT sent
  jsch expecting SSH_MSG_KEXDH_REPLY
  jsch ssh_rsa_verify: signature true
  jsch Host 'info.xyz.com' is known and matches the RSA host key
  jsch SSH_MSG_NEWKEYS sent
  jsch SSH_MSG_NEWKEYS received
  jsch SSH_MSG_SERVICE_REQUEST sent
  jsch SSH_MSG_SERVICE_ACCEPT received
  jsch Authentications that can continue: publickeykeyboard-interactivepassword
  jsch Next authentication method: publickey
  jsch Authentication succeeded (publickey).
  jsch Disconnecting from info.xyz.com port 22

共有2个答案

范哲
2023-03-14

基于Gary关于在重试中添加重试建议的建议。我正在经历以下异常

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'toSftpChannel' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:805)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1278)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207)
    at org.springframework.integration.support.channel.BeanFactoryChannelResolver.resolveDestination(BeanFactoryChannelResolver.java:89)
    at org.springframework.integration.support.channel.BeanFactoryChannelResolver.resolveDestination(BeanFactoryChannelResolver.java:46)
    at org.springframework.integration.gateway.MessagingGatewaySupport.getRequestChannel(MessagingGatewaySupport.java:387)
    at org.springframework.integration.gateway.MessagingGatewaySupport.send(MessagingGatewaySupport.java:422)
    at org.springframework.integration.gateway.GatewayProxyFactoryBean.sendOrSendAndReceive(GatewayProxyFactoryBean.java:568)
    at org.springframework.integration.gateway.GatewayProxyFactoryBean.invokeGatewayMethod(GatewayProxyFactoryBean.java:489)
    at org.springframework.integration.gateway.GatewayProxyFactoryBean.doInvoke(GatewayProxyFactoryBean.java:464)
    at org.springframework.integration.gateway.GatewayProxyFactoryBean.invoke(GatewayProxyFactoryBean.java:453)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
    at com.sun.proxy.$Proxy79.upload(Unknown Source)

我的sftp配置如下

    @Bean
    public DefaultSftpSessionFactory sftpSessionFactory() throws IOException {
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
        //session config here, port, host, private key
      
  }
@Bean
    @ServiceActivator(inputChannel = "toSftpChannel", adviceChain="retryAdvice")
    public SftpMessageHandler uploadHandler() throws IOException {
        final SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
        handler.setRemoteDirectoryExpression(new LiteralExpression(config.getSftpRemoteDirectory()));
        //handler.setChmod(0600);   

        handler.setFileNameGenerator(new FileNameGenerator() {
            @Override
            public String generateFileName(Message<?> message) {
                if (message.getPayload() instanceof File) {
                    logger.info("Files request payload : " + message.getPayload().toString());
                    return ((File) message.getPayload()).getName();
                } else {
                    throw new IllegalArgumentException("File expected as payload.");
                }
            }
        });

        return handler;
    }

    @MessagingGateway
    public interface UploadGateway {
        @Gateway(requestChannel = "toSftpChannel")
        void upload(File file);
    }

    @Bean
    public SimpleRetryPolicy retryPolicy(){
        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
        retryPolicy.setMaxAttempts(5);
        return retryPolicy;
    }

    @Bean
    public FixedBackOffPolicy fixedBackOffPolicy(){
        FixedBackOffPolicy p = new FixedBackOffPolicy();
        p.setBackOffPeriod(1000);
        return p;
    }

    @Bean
    public RequestHandlerRetryAdvice retryAdvice(SimpleRetryPolicy retryPolicy, 
     FixedBackOffPolicy fixedBackOffPolicy){
        RequestHandlerRetryAdvice retryAdvice = new RequestHandlerRetryAdvice();
        RetryTemplate retryTemplate = new RetryTemplate();
        retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
        retryAdvice.setRetryTemplate(retryTemplate);
        return retryAdvice;
    }
贡念
2023-03-14

不清楚你的意思是什么;如果上载失败,将引发异常。

您可以将重试建议添加到SftpMessageHandler以重试。

https://docs.spring.io/spring-integration/docs/5.3.2.RELEASE/reference/html/messaging-endpoints.html#message-处理程序建议链

 类似资料:
  • 我正在使用Spring集成文件/sftp模块,如何避免下载部分文件?我无法控制将文件推送到ftp/sftp的外部进程。

  • 我正在构建一个小微服务来访问来自SFTP文件服务器的文件。我决定使用Spring Integration SFTP完成这项工作。我对Spring Integration很陌生,对它的工作原理很困惑。 我的目标是在SFTP服务器上获得一个目录中的文件列表,并将它们呈现给用户界面。从那里,用户将选择一个文件进行下载,我将使用文件名将文件从SFTP服务器流式传输到用户界面。 其次,我是否需要两个接口才能

  • 我在springboot项目1.5.10版中工作。释放 我正在为sftp使用Spring集成。以下gradle依赖项对我来说一切都很好 我还将普罗米修斯整合到模块中。 我已经完成了普罗米修斯的所有其他要求。但是我没有得到普罗米修斯的指标。 项目中的所有其他服务都在使用prometheus,但没有使用spring integration sftp,prometheus正在为所有这些服务工作。 我尝试

  • 我有一个用例,用户将多个csv文件放到远程目录中,然后放置ready.txt来指示文件已准备好使用。当我们的applcation在远程目录中看到ready.txt文件时,它应该开始使用sftp文件入站通道适配器将所有文件复制到本地目录,包括ready.txt。是否有办法确保readt.txt文件是最后一个要复制到本地目录的文件? 因为当文件从远程目录复制到本地目录时,我有另一个文件入站通道适配器在

  • 我使用Spring集成SFTP将文件上载到SFTP服务器。当我发送一些并发请求或繁重文件时,我收到了套接字克隆异常。但是,当我用简单的Jsch客户端直接发送相同的文件时,即使是并发调用,一切似乎都很好。 我对这两个库都使用默认配置。Spring SFTP使用Jsch,它的配置方式是否会导致错误?Spring是否有比简单Jsch更特殊的配置或操作,但不是在默认配置或行为中?

  • 我需要将不同的文件sftp到服务器上。 在传递范围内的文件位于Windows服务器中。 为此,我考虑使用Spring集成适配器,因为我不仅需要交付文件,而且还需要为每个文件交付将元数据信息写入数据库。 您知道如何使用Spring集成将Windows文件共享上的多个文件sftp到其他服务器吗?