Announcement Announcement Module
Collapse
No announcement yet.
How to set remote director at run time for SFTP Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to set remote director at run time for SFTP

    Hello All,
    I am trying to set remote directory at run time to send a file through SFTP.
    But I am not sure how to do this.
    I am trying to send a file via SFTP using spring integration. I would like to set remote directory at run time but could not figure it out how to do this.
    So in the following code I am setting the userName, hostName, etc .. but cannot figure out how to set the remote directory.

    Thanks in advance

    1) The following is the snippet of the bean configuration
    <si:channel id="outboundFiles"/>
    <bean id="sftpSessionFactory" class="org.springframework.integration.sftp.sessio n.DefaultSftpSessionFactory" />

    <sftp:outbound-channel-adapter session-factory="sftpSessionFactory" channel="outboundFiles" id="sftpOutbound"
    remote-directory="#{systemProperties['remoteDir']}"/>
    2) Code
    public void sendFile(String userName,String hostName,String fileNameURL, String privateKeyFile, String privateKeyPhrase ) {
    logger.info("sending file via sftp to " userName "@" hostName);
    ApplicationContext context =
    new ClassPathXmlApplicationContext(Constants.BEAN_CONF IG_FILE);

    DefaultSftpSessionFactory sftpSessionFactory =
    (DefaultSftpSessionFactory)context.getBean(Constan ts.SftpSessionFactoryBeanName);
    sftpSessionFactory.setHost(hostName);
    sftpSessionFactory.setUser(userName);
    sftpSessionFactory.setPrivateKey(new FileSystemResource(new File(privateKeyFile)));
    sftpSessionFactory.setPrivateKeyPassphrase(private KeyPhrase);

    MessageChannel sftpChannel = (MessageChannel)context.getBean(Constants.MessageC hannelBeanName);

    File file = new File(fileNameURL);
    if(file.exists()){
    Message message=MessageBuilder.withPayload(file).build();
    try {
    sftpChannel.send(message);
    }catch(Exception e){
    if(logger.getLevel() == Level.ALL)
    e.printStackTrace();
    logger.error(" FTP send failed for " fileNameURL " " e.getLocalizedMessage());
    }
    }
    }
Working...
X