Home > OS >  how to set filename and timestamp using spring-integration sftp?
how to set filename and timestamp using spring-integration sftp?

Time:10-12

I need to set filename & timestamp of a file using sftpoutputgateway object. How do i do it ? I know it will be done through Spel language ,but not sure what the systax looks like.

CodePudding user response:

it would be better to just use the SftpRemoteFileTemplate directly in your code.

something like this way.

template.rename(...);
template.get(pathToFile, inputStream -> ...);
template.rename(...); // or template.remove(...);

for timestamp,

 @Bean
    public IntegrationFlow sftpInboundFlow() {
        return IntegrationFlows
            .from(Sftp.inboundAdapter(this.sftpSessionFactory)
                    .preserveTimestamp(true)
                    .remoteDirectory("foo")
                    .regexFilter(".*\\.txt$")
                    .localFilenameExpression("#this.toUpperCase()   '.a'")
                    .localDirectory(new File("sftp-inbound")),
                 e -> e.id("sftpInboundAdapter")
                    .autoStartup(true)
                    .poller(Pollers.fixedDelay(5000)))
            .handle(m -> System.out.println(m.getPayload()))
            .get();
    }
}

you can also refer to this documentation

CodePudding user response:

Setting timestamp on the remote file is not a gateway responsibility.

See SftpRemoteFileTemplate.executeWithClient(ClientCallback<C, T> callback):

        public void handleMessage(Message<?> message) throws MessagingException {
            String remoteFile = (String) message.getPayload();
            Integer newModTime = message.getHeaders().get("newModTime", Integer.class);
            
            template.executeWithClient((ClientCallbackWithoutResult<ChannelSftp>) client -> {
                try {
                    SftpATTRS attrs = client.lstat(remoteFile);
                    attrs.setACMODTIME(attrs.getATime(), newModTime);
                    client.setStat(remoteFile, attrs);
                }
                catch (SftpException e) {
                    throw new RuntimeException(e);
                }
            });
        }

This one can be used in a service activator method where you get access to the Message.

  • Related