0

I am using sftp outbound adaptor to transfer files generated in ItemWriter to sftp server successfully.

Following is the java dsl config for my sftp outbound gateway.

@Bean public IntegrationFlow sftpOutboundFlow() {

    return IntegrationFlows.from("toSftpChannel")
        .handle(Sftp.outboundAdapter(delegatingSessionFactory(sessionFactoryLocator), FileExistsMode.REPLACE)
 .useTemporaryFileName(false)
 .fileNameExpression("headers['" + FileHeaders.FILENAME + "']")
 .remoteDirectoryExpression("headers.path")
         .autoCreateDirectory(true), 
           c -> c.advice(expressionAdvice(c)))                                   .get();
}

/**
 * Advice to remove local files after successful upload
 *   
 * @param c
 * @return
 */
@Bean
public Advice expressionAdvice(GenericEndpointSpec<FileTransferringMessageHandler<ChannelSftp.LsEntry>> c) {
    ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
    advice.setOnSuccessExpressionString("payload.delete()");
    advice.setOnFailureExpressionString("payload + ' failed to upload'");
    advice.setTrapException(true);
    return advice;
}

/**
 * Channel for uploading files
 * 
 *
 */
@MessagingGateway
public interface LettersUploadGateway {
    @Gateway(requestChannel = "toSftpChannel")
    void upload(@Payload File file,  @Header("path") String path);
}

Update: From ItemWriter I am calling gateway's upload method to transfer files as below:

lettersGateway.upload(fileName, batchConfiguration.getLettersDirectory());

The directory specified is remote directory where I want the files to be transfered.

In this process, I have noticed that the temporary local files are created in my project root folder (which are deleted later after successful sftp transfer), is there a way to change the local temporary file location to somewhere like "c:/temp/"?

Thanks for your help.

Mia
  • 169
  • 1
  • 2
  • 11
  • 1
    Looks like you don't include the code which creates files originally. The only temporary folder is used in the `(Sftp.outboundAdapter()` is about really remote one.. – Artem Bilan Jan 28 '19 at 19:14
  • @ArtemBilan updated question to show the ine of code that calls gateway upload method. – Mia Jan 28 '19 at 19:37
  • @ArtemBilan How should I modify (Sftp.outboundAdapter() to specify local file directory to hold temporary files. – Mia Jan 28 '19 at 19:39
  • The `Sftp.outboundAdapter()` (essentially `RemoteFileTemplate`) doesn't modify with local file system. Although I may assume that you use FTP locally, so you are confused that *remote* tmp dir is represented by dir in your local file system. Otherwise it might be the case for an `ItemWriter`. We need to understand who creates that `File` to be sent over your `LettersUploadGateway`. – Artem Bilan Jan 28 '19 at 19:42
  • "fileName" valiable in upload method is actually java.io.File object. This file is generated programatically (in memory) during the batch process which is transferred to SFTP. – Mia Jan 28 '19 at 19:53
  • 1
    Right. And that is exactly the place where you have a temporary directory. There is nothing to do with FTP... – Artem Bilan Jan 28 '19 at 20:08
  • Got it. Thank you!!! I thought it was controlled by the adaptor, my bad.. LOL – Mia Jan 28 '19 at 20:12

0 Answers0