Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.demo;
- import java.io.File;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.builder.SpringApplicationBuilder;
- import org.springframework.context.annotation.Bean;
- import org.springframework.integration.annotation.Gateway;
- import org.springframework.integration.annotation.InboundChannelAdapter;
- import org.springframework.integration.annotation.IntegrationComponentScan;
- import org.springframework.integration.annotation.MessagingGateway;
- import org.springframework.integration.annotation.Poller;
- import org.springframework.integration.annotation.ServiceActivator;
- import org.springframework.integration.config.EnableIntegration;
- import org.springframework.integration.core.MessageSource;
- import org.springframework.integration.file.FileNameGenerator;
- import org.springframework.integration.file.filters.AcceptAllFileListFilter;
- import org.springframework.integration.file.filters.AcceptOnceFileListFilter;
- import org.springframework.integration.file.remote.session.CachingSessionFactory;
- import org.springframework.integration.file.remote.session.SessionFactory;
- import org.springframework.integration.sftp.filters.SftpSimplePatternFileListFilter;
- import org.springframework.integration.sftp.gateway.SftpOutboundGateway;
- import org.springframework.integration.sftp.inbound.SftpInboundFileSynchronizer;
- import org.springframework.integration.sftp.inbound.SftpInboundFileSynchronizingMessageSource;
- import org.springframework.integration.sftp.outbound.SftpMessageHandler;
- import org.springframework.integration.sftp.session.DefaultSftpSessionFactory;
- import org.springframework.messaging.Message;
- import org.springframework.messaging.MessageHandler;
- import org.springframework.messaging.MessagingException;
- import com.jcraft.jsch.ChannelSftp.LsEntry;
- @SpringBootApplication
- @IntegrationComponentScan
- @EnableIntegration
- public class SftpJavaApplication {
- static String msg;
- static File f;
- public static void main(String[] args) {
- new SpringApplicationBuilder(SftpJavaApplication.class).run(args);
- }
- @Bean
- public SessionFactory<LsEntry> sftpSessionFactory() {
- DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
- factory.setHost("localhost");
- factory.setPort(21);
- factory.setUser("ardouz");
- factory.setPassword("rachidneymar");
- factory.setAllowUnknownKeys(true);
- return new CachingSessionFactory<LsEntry>(factory);
- }
- @Bean
- public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
- SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
- fileSynchronizer.setDeleteRemoteFiles(false);
- fileSynchronizer.setRemoteDirectory("C:\\Users\\Ardouz11\\Desktop\\ardouz");
- fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*.*"));
- return fileSynchronizer;
- }
- @Bean
- @InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(fixedDelay = "5"))
- public MessageSource<File> sftpMessageSource() {
- SftpInboundFileSynchronizingMessageSource source = new SftpInboundFileSynchronizingMessageSource(
- sftpInboundFileSynchronizer());
- source.setLocalDirectory(new File("C:\\Users\\Ardouz11\\spring\\demo\\src\\test1"));
- System.out.println("hhhhhhhhhhhhhssssssssssss");
- source.setAutoCreateLocalDirectory(true);
- source.setLocalFilter(new AcceptAllFileListFilter<File>());
- return source;
- }
- @Bean
- @ServiceActivator(inputChannel = "sftpChannel")
- public MessageHandler handler() {
- System.out.println("kkkkkkkkkkkkkkkkkk");
- return new MessageHandler() {
- @Override
- public void handleMessage(Message<?> message) throws MessagingException {
- System.out.println(message.getPayload());
- }
- };
- }
- @Bean
- @ServiceActivator(inputChannel = "sftpChannel")
- public MessageHandler handlerOut() throws MessagingException {
- return new SftpOutboundGateway(sftpSessionFactory(), "put","'C:\\Users\\Ardouz11\\Desktop\\test'");
- }
- /*
- * @Bean
- *
- * @ServiceActivator(inputChannel = "toSftpChannel") public MessageHandler
- * handler1() { SftpMessageHandler handler = new
- * SftpMessageHandler(sftpSessionFactory());
- * handler.setRemoteDirectoryExpressionString("headers['remote-target-dir']");
- * handler.setFileNameGenerator(new FileNameGenerator() {
- *
- * public String generateFileName(Message<?> message) { return
- * "handlerContent.test"; }
- *
- * }); return handler; }
- *
- * @MessagingGateway public interface MyGateway {
- *
- * @Gateway(requestChannel = "toSftpChannel") void sendToSftp(File file);
- *
- * }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement