Search code examples
javaspringapplication.properties

using application.properties values to create a SFTP connection


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


Solution

  • Simply you modify your applicationContext.xml by this adding <context:property-holder>.

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    
           <context:property-placeholder location="classpath:application.properties"/>
    
    </beans>
    

    or if Java based Config, then this :

    @Configuration
    @PropertySource("classpath:application.properties")
    public class Config {
    
    }
    

    Make the class as component.

    @Component
    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();
      }
    
    }