0

How can i get response if file is uploaded succesfuly to sftp? This is the code which i have

@Bean
public SessionFactory<LsEntry> axisSftpSessionFactory() {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
    factory.setHost(axisSftpProperties.getSftpHost());
    factory.setPort(axisSftpProperties.getSftpPort());
    factory.setUser(axisSftpProperties.getSftpUser());
    factory.setPassword(axisSftpProperties.getSftpPassword());
    factory.setAllowUnknownKeys(true);
    return new CachingSessionFactory<>(factory);
}

/**
 * Handler message handler.
 *
 * @return the message handler
 */
@Bean
@ServiceActivator(inputChannel = TO_SFTP_CHANNEL)
public MessageHandler handler() {
    SftpMessageHandler handler = new SftpMessageHandler(axisSftpSessionFactory());
    handler.setRemoteDirectoryExpression(new LiteralExpression(axisSftpProperties.getSftpRemoteDirectory()));
    handler.setFileNameGenerator(message -> (String) message.getHeaders().get(FILENAME));
    return handler;
}


@Component
@MessagingGateway
public interface UploadGateway {


    @Gateway(requestChannel = TO_SFTP_CHANNEL)
    String upload(@Header(FILENAME) String filename, @Payload byte[] bytes);
}

And the idea here is to catch any error if the file is not successfully uploaded to sftp to be able to retry it.

If i use SftpOutboundGateway how can setup a remote directory path? SftpOutboundGateway gateway = new SftpOutboundGateway(sessionFactory(), "put", "payload");

sherybedrock
  • 111
  • 3
  • 11
  • We don't get notifications when you edit the question. Whenever you make an edit you should also comment that you have done so, so that I get a notification. – Gary Russell Aug 19 '19 at 19:20

1 Answers1

0

See the documentation:

The message payload resulting from a put operation is a String that contains the full path of the file on the server after transfer.

Since you have a void return, it is discarded.

So...

String upload(File file);

EDIT

When using the gateway, the third constructor argument is the expression for the file name; the remote directory is provided in the remote file template...

@Bean
public SftpRemoteFileTemplate template() {
    SftpRemoteFileTemplate template = new SftpRemoteFileTemplate(sessionFactory());
    template.setRemoteDirectoryExpression(new LiteralExpression("foo/test"));
    return template;
}

and

new SftpOutboundGateway(template(). "put", "headers['file_name']")

and

System.out.println(gate.upload("foo.txt", "foo".getBytes()));

and

foo/test/foo.txt
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Yes (but don't put code in comments in future - it doesn't render well - it's better to edit the question and add a comment here that you have done so). – Gary Russell Aug 16 '19 at 14:25
  • I'm not able to return the path. What i'm doing wrong? do you have any example? – sherybedrock Aug 16 '19 at 14:46
  • Show the configuration downstream of the gateway (all the way to the SFTP gateway). You need to use an SFTP outbound gateway with the PUT command to get a response - if you are using an outbound channel adapter (`SftpMessageHandler`), that is one-way only. – Gary Russell Aug 16 '19 at 15:01
  • See the edit to my answer for how to use the gateway. – Gary Russell Aug 19 '19 at 19:53