-
Apr 20th, 2011, 03:01 PM
#1
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());
}
}
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules