0

I use spring integration sftp to download and upload files.In the document ,I found

Spring Integration supports sending and receiving files over SFTP by providing three client side endpoints: Inbound Channel Adapter, Outbound Channel Adapter, and Outbound Gateway

When I want to download files I must assign the local directory and when I want to upload files I must assign the remote directory.But if I can't assign the directory when I write the code such as my directory is association with date.How can I assign the directory at runtime?

Here is my code:

@Bean
public SessionFactory<LsEntry> sftpSessionFactory(){
    DefaultSftpSessionFactory defaultSftpSessionFactory = new DefaultSftpSessionFactory();
    defaultSftpSessionFactory.setHost(host);
    defaultSftpSessionFactory.setPort(Integer.parseInt(port));
    defaultSftpSessionFactory.setUser(username);
    defaultSftpSessionFactory.setPassword(password);
    defaultSftpSessionFactory.setAllowUnknownKeys(true);
    return new CachingSessionFactory<LsEntry>(defaultSftpSessionFactory);
}

@Bean
public SftpRemoteFileTemplate sftpRemoteFileTemplate(){
    SftpRemoteFileTemplate sftpRemoteFileTemplate = new SftpRemoteFileTemplate(sftpSessionFactory());
    return sftpRemoteFileTemplate;
}

@Bean
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handlerGet() {
    SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(sftpSessionFactory(), "mget", "payload");
    sftpOutboundGateway.setLocalDirectory(new File(localDirectory));
    sftpOutboundGateway.setFilter(new SftpSimplePatternFileListFilter("*.txt"));
    sftpOutboundGateway.setSendTimeout(1000);
    return sftpOutboundGateway;
}

In the messageHandler,I must assign the localDirectory in the outboundGateway. And when I want change my localDirectory by days.I must download the file to the localDirectory and move to the target directory. How can I assign the localDirectory at runtime .such as today I download to 20170606/ and tomorrow I download to 20170607 ?

edit

this is my option and test

public interface OutboundGatewayOption {
    @Gateway(requestChannel = "sftpChannel")
    public List<File> getFiles(String dir);
}

@Test
public void test2(){
    outboundGatewayOption.getFiles("upload/20160920/");
}
linghu
  • 133
  • 3
  • 16

1 Answers1

0
sftpOutboundGateway.setLocalDirectoryExpression(
    new SpelExpressionParser().parseExpression("headers['whereToPutTheFiles']");

or parseExpression("@someBean.getDirectoryName(payload)")

etc.

The expression must evaluate to a String representing the directory absolute path.

While evaluating the expression, the remote directory is available as a variable #remoteDirectory.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Thans for your reply .Now I replace the setLocalDirectory with `sftpOutboundGateway.setLocalDirectoryExpression(new SpelExpressionParser().parseExpression("headers['/Users/linghu/testfile/']"));`.And when I run the test to download file ,the test passed but no file is download.where is wrong. I don't understand what this works clearly.could you give me some samples? – linghu Apr 07 '17 at 06:49
  • You seem to have misunderstood, if you want a variable then you need an expression where the result can change for each message. To use a `headers` expression, you will need to set the header upstream of the gateway, perhaps with a header enricher, to set, `myHeader` to `'/Users/ling‌​hu/testfile/'` before sending the message to the gateway; then use `headers['myHeader;]`. What you have will try to find the value from a header named `/Users/ling‌​hu/testfile.`. If you are using a messaging gateway to send the message from a java program to the gateway, you can set the header there. – Gary Russell Apr 07 '17 at 12:38
  • I'm sorry to reply so late.I find this links [spring integration sftp xml sample](https://github.com/spring-projects/spring-integration/blob/master/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpServerOutboundTests-context.xml). And I set localDirectory with `sftpOutboundGateway.setLocalDirectoryExpression(new SpelExpressionParser().parseExpression("'download'+#remoteDirectory"));`.Now it works ,thanks a lot of your reply. – linghu Apr 24 '17 at 10:14