my application.properties
file look like this:
sftp.host=0.0.0.0
sftp.port=22
sftp.user=foo
sftp.password=pass
and my upload class with upload method looks like this:
public class UpAndDownLoad {
@Value("${sftp.host}")
private String sftpHost;
@Value("${sftp.port}")
private int sftpPort;
@Value("${sftp.user}")
private String sftpUser;
@Value("${sftp.password}")
private String sftpPasword;
private DefaultSftpSessionFactory getSftpFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
factory.setHost(sftpHost);
factory.setPort(sftpPort);
factory.setAllowUnknownKeys(true);
factory.setUser(sftpUser);
factory.setPassword(sftpPasword);
return factory;
}
public void upload() throws IOException {
SftpSession session = getSftpFactory().getSession();
InputStream resourceAsStream = UpAndDownLoad.class.getClassLoader().getResourceAsStream("mytextfile.txt");
session.write(resourceAsStream, "upload/mynewfile" + LocalDateTime.now() + ".txt");
session.close();
}
Whenever I type out the values of sftp host, user, password the upload method works meticulously.
private DefaultSftpSessionFactory getSftpFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
factory.setHost("0.0.0.0");
factory.setPort(22);
factory.setAllowUnknownKeys(true);
factory.setUser("foo");
factory.setPassword("passs");
return factory;
}
But as soon as i pass in the vlaues from the application properties, like shown at the second code block, it fails with:
java.lang.IllegalStateException: failed to create SFTP Session