Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.57 KB | None | 0 0
  1. package com.example.demo;
  2.  
  3. import java.io.File;
  4.  
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.boot.builder.SpringApplicationBuilder;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.integration.annotation.Gateway;
  9. import org.springframework.integration.annotation.InboundChannelAdapter;
  10. import org.springframework.integration.annotation.IntegrationComponentScan;
  11. import org.springframework.integration.annotation.MessagingGateway;
  12. import org.springframework.integration.annotation.Poller;
  13. import org.springframework.integration.annotation.ServiceActivator;
  14. import org.springframework.integration.config.EnableIntegration;
  15. import org.springframework.integration.core.MessageSource;
  16. import org.springframework.integration.file.FileNameGenerator;
  17. import org.springframework.integration.file.filters.AcceptAllFileListFilter;
  18. import org.springframework.integration.file.filters.AcceptOnceFileListFilter;
  19. import org.springframework.integration.file.remote.session.CachingSessionFactory;
  20. import org.springframework.integration.file.remote.session.SessionFactory;
  21. import org.springframework.integration.sftp.filters.SftpSimplePatternFileListFilter;
  22. import org.springframework.integration.sftp.gateway.SftpOutboundGateway;
  23. import org.springframework.integration.sftp.inbound.SftpInboundFileSynchronizer;
  24. import org.springframework.integration.sftp.inbound.SftpInboundFileSynchronizingMessageSource;
  25. import org.springframework.integration.sftp.outbound.SftpMessageHandler;
  26. import org.springframework.integration.sftp.session.DefaultSftpSessionFactory;
  27. import org.springframework.messaging.Message;
  28. import org.springframework.messaging.MessageHandler;
  29. import org.springframework.messaging.MessagingException;
  30.  
  31. import com.jcraft.jsch.ChannelSftp.LsEntry;
  32.  
  33. @SpringBootApplication
  34. @IntegrationComponentScan
  35. @EnableIntegration
  36. public class SftpJavaApplication {
  37.     static String msg;
  38.     static File f;
  39.  
  40.     public static void main(String[] args) {
  41.         new SpringApplicationBuilder(SftpJavaApplication.class).run(args);
  42.    
  43.  
  44.     }
  45.  
  46.     @Bean
  47.     public SessionFactory<LsEntry> sftpSessionFactory() {
  48.         DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
  49.         factory.setHost("localhost");
  50.         factory.setPort(21);
  51.         factory.setUser("ardouz");
  52.         factory.setPassword("rachidneymar");
  53.         factory.setAllowUnknownKeys(true);
  54.         return new CachingSessionFactory<LsEntry>(factory);
  55.     }
  56.  
  57.     @Bean
  58.     public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
  59.         SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
  60.         fileSynchronizer.setDeleteRemoteFiles(false);
  61.         fileSynchronizer.setRemoteDirectory("C:\\Users\\Ardouz11\\Desktop\\ardouz");
  62.         fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*.*"));
  63.         return fileSynchronizer;
  64.     }
  65.  
  66.     @Bean
  67.     @InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(fixedDelay = "5"))
  68.     public MessageSource<File> sftpMessageSource() {
  69.         SftpInboundFileSynchronizingMessageSource source = new SftpInboundFileSynchronizingMessageSource(
  70.                 sftpInboundFileSynchronizer());
  71.  
  72.         source.setLocalDirectory(new File("C:\\Users\\Ardouz11\\spring\\demo\\src\\test1"));
  73.         System.out.println("hhhhhhhhhhhhhssssssssssss");
  74.         source.setAutoCreateLocalDirectory(true);
  75.        
  76.         source.setLocalFilter(new AcceptAllFileListFilter<File>());
  77.         return source;
  78.     }
  79.    
  80.  
  81.     @Bean
  82.     @ServiceActivator(inputChannel = "sftpChannel")
  83.     public MessageHandler handler() {
  84.         System.out.println("kkkkkkkkkkkkkkkkkk");
  85.         return new MessageHandler() {
  86.             @Override
  87.             public void handleMessage(Message<?> message) throws MessagingException {
  88.                 System.out.println(message.getPayload());
  89.  
  90.             }
  91.  
  92.         };
  93.  
  94.     }
  95.  
  96.     @Bean
  97.     @ServiceActivator(inputChannel = "sftpChannel")
  98.     public MessageHandler handlerOut() throws MessagingException  {
  99.         return new SftpOutboundGateway(sftpSessionFactory(), "put","'C:\\Users\\Ardouz11\\Desktop\\test'");
  100.     }
  101.  
  102.     /*
  103.      * @Bean
  104.      *
  105.      * @ServiceActivator(inputChannel = "toSftpChannel") public MessageHandler
  106.      * handler1() { SftpMessageHandler handler = new
  107.      * SftpMessageHandler(sftpSessionFactory());
  108.      * handler.setRemoteDirectoryExpressionString("headers['remote-target-dir']");
  109.      * handler.setFileNameGenerator(new FileNameGenerator() {
  110.      *
  111.      * public String generateFileName(Message<?> message) { return
  112.      * "handlerContent.test"; }
  113.      *
  114.      * }); return handler; }
  115.      *
  116.      * @MessagingGateway public interface MyGateway {
  117.      *
  118.      * @Gateway(requestChannel = "toSftpChannel") void sendToSftp(File file);
  119.      *
  120.      * }
  121.      */
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement