Sftp using spring integration?

Viewed 18

After connecting to the FTP server using FileZilla using creds, I have a directory structure in the FTP location as /APP/PGLT01/TECH/ in TECH files are present /APP/PGLT01/TECH/user.txt, and sometimes there is no file.

I've been trying to use SftpInboundFileSynchronizer. What should I put in the remote directory and what should I put in the local directory? For now, I have put the same

When we are running the application we are getting this FileNotFoundException: TECH then then application getting stopped.

Can we use FileWritingMessageHandler if we are able to read successfully from source sftp ?

    public DefaultSftpSessionFactory gimmeFactory(){
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
        factory.setHost("0.0.0.0");
        factory.setPort(22);
        factory.setAllowUnknownKeys(true);
        factory.setUser("mike");
        factory.setPassword("password123");
        return factory;
    }

    @Bean(name="mydefaultsync")
    public SftpInboundFileSynchronizer synchronizer(){
        SftpInboundFileSynchronizer sync = new SftpInboundFileSynchronizer(gimmeFactory());
        sync.setDeleteRemoteFiles(false);
        sync.setRemoteDirectory("/upload/done");
        sync.setFilter(new SftpSimplePatternFileListFilter("*.txt"));
        return sync;
    }

    @Bean(name="sftpMessageSource")
    @InboundChannelAdapter(channel="fileuploaded", poller = @Poller(fixedDelay = "3000"))
    public MessageSource<File> sftpMessageSource(){
        SftpInboundFileSynchronizingMessageSource source =
                new SftpInboundFileSynchronizingMessageSource(synchronizer());
        source.setLocalDirectory(new File("/upload/done"));
        source.setAutoCreateLocalDirectory(false);
        source.setMaxFetchSize(1);
        return source;
    }


    @ServiceActivator(inputChannel = "fileuploaded")
    public void handleIncomingFile(File file) throws IOException {
        log.info(String.format("handleIncomingFile BEGIN %s", file.getName()));
        String content = FileUtils.readFileToString(file, "UTF-8");
        log.info(String.format("Content: %s", content));
        log.info(String.format("handleIncomingFile END %s", file.getName()));
    }

0 Answers
Related