使用 application.properties 值创建 SFTP 连接

using application.properties values to create a SFTP connection

我的 application.properties 文件如下所示:

sftp.host=0.0.0.0
sftp.port=22
sftp.user=foo
sftp.password=pass

我的上传 class 使用上传方法如下所示:

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

每当我输入 sftp 主机、用户、密码的值时,上传方法都非常有效。

  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;
  }

但是,一旦我从应用程序属性中传递值,如第二个代码块所示,它就会失败: java.lang.IllegalStateException: failed to create SFTP Session

只需通过添加 <context:property-holder> 来修改 applicationContext.xml

<?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>

或者如果 Java 基于配置,那么这个:

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

}

将 class 作为组件。

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

}

如果您希望 @Value 注释起作用,您应该 spring 管理此对象。添加 @Component@Autowire

更好的方法是创建单独的 SftpProperties 作为 @ConfigurationProperties

@Component
@ConfigurationProperties("sftp")
public class SftpProperties {

    private String host;
    private int port;
    private String user;
    private String password;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    @Override
    public String toString() {
        return "SftpProperties{" +
                "host='" + host + '\'' +
                ", port=" + port +
                ", user='" + user + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

您的 application.properties

中将有更多的声明性配置和自动完成功能
@Component
public class UpAndDownLoad {

    final
    SftpProperties props;

   @Autowired
    public UpAndDownLoad(SftpProperties props) {
        this.props = props;
    }
    private DefaultSftpSessionFactory getSftpFactory() {
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
        factory.setHost(props.getHost());
        factory.setPort(props.getPort());
        factory.setAllowUnknownKeys(true);
        factory.setUser(props.getUser());
        factory.setPassword(props.getPassword());
        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();
    }
}